ClassName.attribute vs self.__class__.attribute

David C. Ullrich dullrich at sprynet.com
Fri Jun 6 18:03:56 CEST 2008


In article <mailman.154.1212748011.1044.python-list at python.org>,
 Gabriel Rossetti <gabriel.rossetti at arimaz.com> wrote:

> Larry Bates wrote:
> > Gabriel Rossetti wrote:
> >> Hello everyone,
> >>
> >> I had read somewhere that it is preferred to use 
> >> self.__class__.attribute over ClassName.attribute to access class 
> >> (aka static) attributes. I had done this and it seamed to work, until 
> >> I subclassed a class using this technique and from there on things 
> >> started screwing up. I finally tracked it down to 
> >> self.__class__.attribute! What was happening is that the child 
> >> classes each over-rode the class attribute at their level, and the 
> >> parent's was never set, so while I was thinking that I had indeed a 
> >> class attribute set in the parent, it was the child's that was set, 
> >> and every child had it's own instance! Since it was a locking 
> >> mechanism, lots of fun to debug... So, I suggest never using 
> >> self.__class__.attribute, unless you don't mind it's children 
> >> overriding it, but if you want a truly top-level class attribute, use 
> >> ClassName.attribute everywhere!

I shouldn't butt in since everyone else knows more about
this than I do, but it seems to me that saying you should
do this is wrong and saying you should do that is wrong -
which you should do depends on what you're trying to
accomplish.

There's something that comes up all the time in stuff
I do, where implicitly accessing self.__class__.attribute
is vital to make it work right. Say I have a Matrix class,
with an __add__ method:

class Matrix:
  def __init__(self, data):
    whatever
  def __add__(self, other):
    newdata = whatever
    return Matrix(newdata)

The last line is almost surely not what I want (took
me a while, long ago, to figure this out). Because someday
when I have a subclass I want __add__ to return an instance
of the subclass. At first I thought I needed to rewrite the
last line to return SubClass(newdata). No, I simply return
self.__class__(newdata) and it works exactly the way I want.

> >> I wish books and tutorials mentioned this explicitly....
> >>
> >> Gabriel
> >
> > If you define a class instance variable with the same name as the 
> > class attribute, how would Python be able to distinguish the two?  
> > That is a feature not a problem.  Getter looks for instance attribute, 
> > if one is not found it looks for a class attribute, and upwards.  This 
> > behavior is used by Zope to do all sorts of neat stuff.
> >
> > -Larry Bates
> > -- 
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
> A class instance variable, you must mean an instance attribute no? If 
> that is so, then with just self.attribute? Maybe there is a concept that 
> I don't know about, I've studied class/static attributes and instance 
> attributes in my OOP classes.
> 
> Gabriel

-- 
David C. Ullrich



More information about the Python-list mailing list