ClassName.attribute vs self.__class__.attribute

Terry Reedy tjreedy at udel.edu
Fri Jun 13 17:39:27 EDT 2008


"> One metaclass (i.e. type) has a class attribute that refers to itself.
 >
> Other metaclasses have a class attribute that refers to the metaclass's
> metaclass. I can't think of any situation where a metaclass would be its
> own metaclass except for 'type' itself, but then I think I've got a
> headache trying to think about this

In 3.0, with old classes gone and all classes called 'classes'*:

All objects are instances of base class 'object', nearly always indirectly 
via subclasses of 'object'.  All classes are subclasses of 'object', 
including base metaclass 'type'.  The methods of 'object' are the methods 
that apply to  objects as objects in general, as instances.  Many of these 
are 'base methods' that get overridden in subclasses to fit their 
instances.

All classes (including object and type, dropping the quotes) are instances 
of type, usually directly but occassionally via subclasses of type.  In 
other words, the subclass type defines the subclass of objects that are 
classes.  The additional attributes and methods of type (beyond those 
inherited from object ) are, therefore, those that specifically apply to 
all classes.  These include __call__, by which classes (including 'object' 
and 'type' itself) make instances.

The circularity conundrum is that type is a subclass of object while the 
immutable class attribute of both object and type is type.  The answer, of 
course, is that a Python interpreter, as god of a Python universe, creates 
both 'ex nihilo', from nothing, with the required attributes.

* In 3.0a5+
>>> type(type)
<class 'type'> # instead of <type 'type'>, and so on.

Terry Jan Reedy






More information about the Python-list mailing list