How can 'type' be an instance of itself?

LittleGrasshopper seattlehanks at yahoo.com
Thu May 28 19:46:34 EDT 2009


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

That puts my mind at ease, thank you!



More information about the Python-list mailing list