How can 'type' be an instance of itself?

LittleGrasshopper seattlehanks at
Thu May 28 20:20:18 EDT 2009

On May 28, 4:37 pm, Christian Heimes <li... at> wrote:
> 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

And just to clarify that I do really understand what this means, I
gather that what it entails is that 'type' is an instance of itself
just from a conceptual point of view. In other words, the code for the
(meta)class 'type' is created statically by object code (compiled C
code) before the python runtime is initiated (you referred to it as
the interpreter bootstrapping phase.) I also guess that this code sets
__class__ and __metaclass__ to the 'type' object itself (I am guessing
a self-referencial pointer in C.) While I just am hypothesizing on the
details, please let me know if you sense that I have misunderstood any
essential part of your explanation.



More information about the Python-list mailing list