classmethods, class variables and subclassing

Andrew Jaffe a.jaffe-usenet at bakerjaffe.plus.com
Fri Oct 21 02:31:57 EDT 2005


> 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.

A



More information about the Python-list mailing list