ClassName.attribute vs self.__class__.attribute
Gabriel Rossetti
gabriel.rossetti at arimaz.com
Fri Jun 13 02:33:52 EDT 2008
Duncan Booth wrote:
> Mike Orr <sluggoster at gmail.com> 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):
>
>
Ok, I didn't know that
>>>> 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
> type(x)
> to access its class rather than accessing the __class__ attribute directly
> that doesn't particularly matter.
>
>
Yes, I don't use old style classes
More information about the Python-list
mailing list