super, object and type?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Aug 12 14:04:26 EDT 2008


ssecorp a écrit :
>>>> super(object, type)
> <super: <class 'object'>, <type object>>
>>>> super(type, object)
> <super: <class 'type'>, <type object>>
> 
> how can both work?
> 
> they can't both be the superclass of each other right? or is it some
> sort of mutually recursive definition?
> 

some sort, yes:

> 
> 
>>>> help(object)
> Help on class object in module __builtin__:
> 
> class object
>  |  The most base type
> 
>>>> help(type)
> Help on class type in module __builtin__:
> 
> class type(object)
>  |  type(object) -> the object's type
>  |  type(name, bases, dict) -> a new type

You forgot:
 >>> type(object)
<type 'type'>
 >>>
 >>> type(type)
<type 'type'>
 >>>

<apply-only to="new-style classes">

As you can see, object is both the superclass *and* an instance of type, 
and type is an instance of itself.

This kind of situation is common in OOPLs having metaclasses and a 
unique common base class.

A class is an object, so it have to be an instance of a class. If you 
don't make the base metaclass an instance of itself, you have an 
infinite recursion. Also, since a class is an object and there's one 
unique common base class for all other classes, the base metaclass has 
to subclass this class (else it wouldn't be the unique common base 
class...).

</apply-only>

HTH



More information about the Python-list mailing list