ClassName.attribute vs self.__class__.attribute

Casey McGinty casey.mcginty at gmail.com
Fri Jun 6 16:43:15 EDT 2008


On Thu, Jun 5, 2008 at 11:39 AM, Terry Reedy <tjreedy at udel.edu> wrote:

>
> If you want to access the attribute of a particular class, to read or
> write, use that class.
>   SomeClass.attr
> Note that no instance is required or relevant.
>
> If you want to read the attrubute of the class of an instance (or the first
> superclass with the attribute, whatever that class might be, use self.attr
> or self.__class__.attr.  (Use the latter if the instance has (or might
> have) an attribute of the same name).
>
> For setting an attribute, self.attr = x sets it on the instance while
> self.__class__.attr = x sets it on its class.
>  <http://mail.python.org/mailman/listinfo/python-list>
>

So the key here is that obj.__class__.attr  will only access  the immediate
child class name space, but SuperClass.attr will guarantee your are access
the first super class namespace? If that is true, then the bug makes sense.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080606/d65f0ff7/attachment-0001.html>


More information about the Python-list mailing list