An object is an instance (or not)?

Gregory Ewing greg.ewing at canterbury.ac.nz
Wed Jan 28 05:33:43 EST 2015


Mario Figueiredo wrote:
> I couldn't think of a way to 
> demonstrate that a class object does not participate in its own 
> inheritance rules. Only instances of it can.

I think I may see where your reasoning is going astray.
You think that an instance "inherits" methods from its
class in the same way that a subclass inherits methods
from its base class.

But thinking of the instance-class relationship as
an inheritance relationship is misleading. It's more
accurate to say that a class *defines* the methods
that its instances have. The class inherits methods
from its base class, but those methods still apply to
instances of the class, not the class itself.

That doesn't mean the class itself can't be given
methods, though. The methods of the class are defined
by its metaclass, and the metaclass inherits methods
from *its* base class, etc.

Here's a diagram:

+------+                  +------------+
| type |                  | base class |
+------+                  +------------+
    ^                           ^
    | subclass of               | subclass of
    |                           |
+-----------+              +-------+              +----------+
| metaclass |<-------------| class |<-------------| instance |
+-----------+  instance of +-------+  instance of +----------+

It should be clear from this that the relationship
between an instance and its class is exactly the same
as that between a class and its metaclass, including
inheritance relationships.

The diagram can be extended indefinitely far to the
left -- the metaclass could be an instance of a
meta-metaclass, etc. (Although there's an old standing
joke in the Python world that metaclasses make your
head explode, so I hate to think what a meta-metaclass
would do -- probably take out a whole floor of the
office building you work in. Meta-meta-metaclasses are
right out.)

-- 
Greg



More information about the Python-list mailing list