[2.2b2]: class variable and classmethod, subclass

{-- Rot13 - Hateme ungrzr2 at ubatxbat.pbz
Tue Nov 27 06:44:24 CET 2001


In 2.2B2
Inside a class,I did a change to a class variable inside the class'
classmethod. If the variable has not been changed in the subclass before,
the class variable in the subclass of the class is also changed. Is this
behavior intentional or a bug? 

There is no such behavior if the change is done in a normal member
method.

The code:
=====
class A:
	_test = 'start'
	def change(cls, str="changed"):
		cls._test = str
		print cls, "change:", cls._test
	change = classmethod(change)
	def val(self):
		print self.__class__, "val:", self._test

class B(A):
	def val(self):
		print self.__class__, "val:", self._test
		
A().val()
B().val()
A().change("Changed by A 1") 
B().val()
A().change("Changed by A 2") 
B().val()
B().change("Changed by B")
B().val()
A().change("Changed by A 1") 
B().val()

===> output:
__main__.A val: start
__main__.B val: start
__main__.A change: Changed by A 1
__main__.B val: Changed by A 1
__main__.A change: Changed by A 2
__main__.B val: Changed by A 2
__main__.B change: Changed by B
__main__.B val: Changed by B
__main__.A change: Changed by A 1
__main__.B val: Changed by B



More information about the Python-list mailing list