ClassName.attribute vs self.__class__.attribute

Duncan Booth duncan.booth at invalid.invalid
Thu Jun 12 10:12:00 CEST 2008

Mike Orr <sluggoster at> wrote:

> That's a misunderstanding of classes vs instances.  If you have an
> instance of MyClass(Superclass), there is one instance but several
> classes.  The instance is of MyClass; there is no instance of
> Superclass.  'self' has a .__class__ attribute because it's an
> instance, but MyClass and Superclass do not because they're already
> classes.

Classes are also instances, usually they are instances of the type 'type' 
(and even 'type' is an instance of itself):

>>> class SuperClass(object): pass

>>> SuperClass.__class__
<type 'type'>
>>> type(SuperClass)
<type 'type'>
>>> type.__class__
<type 'type'>

Old style classes don't have a class attribute, but you shouldn't be using 
old style classes anyway and so long as you use 
to access its class rather than accessing the __class__ attribute directly 
that doesn't particularly matter.

Duncan Booth

More information about the Python-list mailing list