classmethods, class variables and subclassing

Steve Holden steve at holdenweb.com
Fri Oct 21 09:19:59 CEST 2005


Andrew Jaffe wrote:
>>Andrew Jaffe wrote:
>>
>>
>>>Hi,
>>>
>>>I have a class with various class-level variables which are used to 
>>>store global state information for all instances of a class. These are 
>>>set by a classmethod as in the following
>>>
>>>class sup(object):
>>>     cvar1 = None
>>>     cvar2 = None
>>>
>>>     @classmethod
>>>     def setcvar1(cls, val):
>>>         cls.cvar1 = val
>>>
>>>     @classmethod
>>>     def setcvar2(cls, val):
>>>         cls.cvar2 = val
>>>
>>>     @classmethod
>>>     def printcvars(cls):
>>>    print cls.cvar1, cls.cvar2
>>>
>>>Now, the problem comes when I want to subclass this class. If I 
>>>override the setcvar1 method to do some new things special to this 
>>>class, and then call the sup.setcvar1() method, it all works fine:
>>>
>>>class sub(sup):
>>>     cvar1a = None
>>>
>>>     @classmethod
>>>     def setcvar1(cls, val, vala):
>>>         cls.cvar1a = vala
>>>         sup.setcvar1(val)
>>>
>>>     @classmethod
>>>     def printcvars(cls):
>>>         print cls.cvar1a
>>>         sup.printcvars()
>>>
>>>This works fine, and sets cvar and cvar2 for both classes.
>>>
>>>However, if  I *don't* override the setcvar2 method, but I call 
>>>sub.setcvar2(val) directly, then only sub.cvar2 gets set; it is no 
>>>longer identical to sup.cvar1!
>>>
>>>In particular,
>>>     sub.setcvar1(1,10)
>>>     sub.setcvar2(2)
>>>     sub.printcvars()
>>>prints
>>>   10
>>>   1 None
>>>
>>>i.e. sub.cvar1, sub.cvar1a, sub.cvar2= 1 10 2
>>>but sup.cvar1, cvar2= 1 None
>>>
>>>This behavior is "expected", but is it desirable?
>>>
>>
>>You are experiencing this problem because you are using hard-wired class 
>>names. Try using (for example) self.__class__. That way, even if your 
>>method is inheroted by a subclass it will use the class of the object it 
>>finds itself a method of. No need to use classmethods.
> 
> 
> The problem is that I actually do want to call these methods on the 
> class itself, before I've made any instances.
> 
I see. I think. So what you are saying is that when you call 
sup.printcvars() from inside a sub method you want it to see the 
namespace of the sub class not the sup?

Since you have set all this up carefully you must have a use case, but 
it seems a little contorted (to me). Basically you appear to want the 
classes to behave statically the way that instances do dynamically?

Not sure I can help you here.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list