您需要使用完整的类名来设置类变量. double_x和tripple_x中的cls将引用子类(分别为ObjectOne和ObjectTwo),并且这些子类上的设置属性将存储新变量,而不是更改类变量BaseObject.x.您只能通过直接访问它们来更改基类变量.
使用您的代码,我们得到:
>>> obj_1 = ObjectOne()
cls.initialized = False
>>> obj_1.double_x()
2
>>> obj_2 = ObjectTwo()
cls.initialized = False
>>> obj_2.triple_x()
3
>>> BaseObject.x
Traceback (most recent call last):
File "", line 1, in
AttributeError: type object 'BaseObject' has no attribute 'x'
>>> BaseObject.initialized, ObjectOne.initialized, ObjectOne.x, ObjectTwo.initialized, ObjectTwo.x
(False, True, 2, True, 3)
发生了什么,在_initialize()中,cls被设置为ObjectOne或ObjectTwo,具体取决于您创建的实例,并且每个子类都有自己的初始化变量和x的副本.
使用BaseObject._initialize()(以确保初始化BaseObject而不是子类)给出:
>>> obj_1 = ObjectOne()
cls.initialized = False
>>> obj_1.double_x()
2
>>> obj_2 = ObjectTwo()
cls.initialized = True
>>> obj_2.triple_x()
3
>>> BaseObject.x, ObjectOne.x, ObjectTwo.x
(1, 2, 3)
>>> BaseObject.initialized
True
>>> 'x' in ObjectOne.__dict__
True
>>> 'initialized' in ObjectOne.__dict__
False
>>> 'initialized' in ObjectTwo.__dict__
False
所以现在_initialize()使用BaseObject作为设置初始化的目标和x的初始值,但是double_x和triple_x仍然使用它们自己的子类来设置x的新值,并且不通过BaseObject共享该值.
在特定基类上设置类变量的唯一选择是直接在所有类方法中引用它:
class BaseObject(object):
initialized = False
def __init__(self):
BaseObject._initialize()
@classmethod
def _initialize(cls):
print "cls.initialized = "+str(cls.initialized)
if not cls.initialized:
cls.x = 1
cls.initialized = True
class ObjectOne(BaseObject):
@classmethod
def double_x(cls):
BaseObject.x = BaseObject.x * 2
print cls.x
class ObjectTwo(BaseObject):
@classmethod
def triple_x(cls):
BaseObject.x = BaseObject.x * 3
print cls.x
这会给:
>>> obj_1 = ObjectOne()
cls.initialized = False
>>> obj_1.double_x()
2
>>> obj_2 = ObjectTwo()
cls.initialized = True
>>> obj_2.triple_x()
6
请注意,我调用了BaseObject._initialize()来确保cls是BasObject而不是子类.然后,当设置x时,double_x和triple_x方法仍然直接引用BaseObject以确保直接在基类上设置变量.当读取x的值时,上面的例子仍然使用cls,当没有在本地设置时,它使用类MRO在基类上找到x.