An object is an instance (or not)?

Ian Kelly ian.g.kelly at gmail.com
Wed Jan 28 03:15:57 CET 2015


On Tue, Jan 27, 2015 at 5:17 PM, Mario Figueiredo <marfig at gmail.com> wrote:
> In article <mailman.18191.1422400930.18130.python-list at python.org>,
> ned at nedbatchelder.com says...
>> What does "participate in OOP" mean?
>
> Means the object is capable of participating in inheritance and/or
> polymorphism. An instance of an object is capable of doing so, per its
> class definitions. Whereas a Python class object is not.

Python class objects are capable of doing both these things.

>     >>> class Master:
>             def func(self):
>                 pass
>
>     >>> class Sub(Master):
>             pass
>
>     >>> Sub.func()
>     TypeError: func() missing 1 required positional argument: 'self'

You get the same result by calling Master.func(), so I don't see how
this is a counter-example. Anyway, here is how class objects
participate in inheritance:

class BaseMetaClass(type):
    def func(cls):
        return 42

class DerivedMetaClass(BaseMetaClass):
    pass

class DerivedClass(metaclass=DerivedMetaClass):
    pass

>>> isinstance(DerivedClass, BaseMetaClass)
True
>>> print(DerivedClass.func())
42

Note that this is exactly the same way that non-class objects
participate in inheritance. Instances don't simply inherit from other
instances. Rather, the class of the instance inherits from other
classes and provides methods to the instances. And that's what happens
here: the class's meta class inherits from another, and the method it
defines is callable on the instance.



More information about the Python-list mailing list