3 ms·
classes are objects, and inheritance is just the __mro__ on a class -- ie., its prototypal chain if you want to inherit from an object, just construct a class
by mjburgess 6y ago
classes are objects, and inheritance is just the __mro__ on a class -- ie., its prototypal chain
if you want to inherit from an object, just construct a class object from it using type().
Eg., at least, for any given object `me`,
type('ChildOfMe', (type(me),), vars(me))
But if you wanted a more literal dispatch to `me` you could create a proxy class which dispatched to a reference to `me`.
type('ChildOfMe', (proxyclass(me),), {})
Where `proxyclass` is a function of the form,
def proxyclass(obj):
class Proxy:
... dispatch to obj ...
return Proxy
- chc 6y ago"If you want to inherit from an object, just create a class based on that object" is a description of how you'd imitate prototype inheritance in a language that only has class-based inheritance. It does not mean you're actually looking at a prototype-based language, and in fact strongly suggests the opposite. You can't get inheritance in Python without engaging with the class system. I suppose you could just say that classes are a particularly restricted sort of prototypes, and thus any class-based language is effectively prototype-based. But at that point "<OOP language> has prototype inheritance" is just tautological based on the set of definitions you've chosen.
- mjburgess 5y agoIt's not tautological. I don't think C#, C++, Java, etc. can have classes proxy for objects. Classes there aren't objects. You cannot write "return NewClass". Python is prototypal in just the same way js is: inheritance is a daisy-chain of references between objects. JS is "maximally prototypal" if you like, where any object may be refernced. In python, only objects of type "type": but that isn't a big restriction, as shown.