How can 'type' be an instance of itself?

Christian Heimes lists at cheimes.de
Fri May 29 01:37:12 CEST 2009


LittleGrasshopper wrote:
> This is probably trivial, but it's driving me mad somehow. All (new
> style) classes are instances of 'type' by default, unless a custom
> metaclass is specified. I take this to mean that when a class
> declaration is found in the code, an instance of 'type' representing
> that class is created by calling type.__init__. What really gets me is
> how can 'type' be an instance of itself. In order to create 'type' we
> would need to call type.__init__, but it seems at this point it
> wouldn't exist. Probably a dumb question, which I hope someone can
> explain in some detail.

The classes 'type' and 'object' are written in C. You can do things in C
code that aren't possible from pure Python code. The circular
dependencies between 'type' and 'object' are created during the boot
strapping phase of the interpreter.

>>> type(type)
<type 'type'>
>>> type.__bases__
(<type 'object'>,)
>>> object.__bases__
()
>>> type(object)
<type 'type'>

Christian




More information about the Python-list mailing list