The object named 'type', in 2.2

Philip Swartzleonard starx at pacbell.net
Sat Feb 2 23:38:36 EST 2002


I would like to know just where i can figure out the logic behind this, 
because of the name i can't seem to do a meaningful search on this. Here 
is what i know:

1. In Python 2.2, there is an builtin object in python named 'type'. It 
is of type 'type'.

>>> type
<type 'type'>


2. Every object that the dot operator can be used on, except for classic 
classes, has 'type' as it's ancestor that can be accessed through 
__class__ at some level.

>>> type
<type 'type'>
>>> [5].__class__ is type
0
>>> [5].__class__.__class__ is type
1
>>> class x(object): pass
>>> x.__class__ is type
1
>>> a = x()
>>> a.__class__ is x
1
>>> a.__class__.__class__ is type
1
>>> class y: pass
>>> y.__class__
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in ?
    y.__class__
AttributeError: class y has no attribute '__class__'


3. Type is it's own parent:

>>> type.__class__ is type
1


It is this last one particularly that I do not understand. I ran into it 
today trying to fix a recrusion-death problem in PyCrust under 2.2 -- it 
recursivly accesses thing's __class__es in it's attribute-finding 
routines. I've tried to find information on this, but it has eluded me so 
far...

Thanks

-- 
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com



More information about the Python-list mailing list