type, object hierarchy?

Christian Heimes lists at cheimes.de
Mon Feb 4 15:48:17 CET 2008


7stud wrote:
> The output suggests that Dog actually is a subclass of type--despite
> the fact that issubclass(Dog, type) returns False.  In addition, the
> output of dir(type) and dir(object):

No, type is the meta class of the class object:

>>> issubclass(object, type)
False
>>> isinstance(object, type)
True

As you can see object is not a subclass of type but an instance of type.
This may look confusing at first but it's easy to explain. Like a class
is the blue print of an instance, a meta class is the blue print of a
class. In Python everything is a direct or indirect instance of type.

o = someobject
while o is not type:
    o = type(o)
    print o

The code will eventually print "type".

Christian




More information about the Python-list mailing list