我有一个类,它存储一个变量,然后当该变量更改为指定的值时,它应该能够执行作为参数传递的给定代码段。我试着这样做:
class SharedAttribute(object):
def __init__(self, value, onEquals={}):
self.value = value
self.onEquals = onEquals
def __setattr__(self, key, val):
super(SharedAttribute, self).__setattr__(key, val) #Set the attribute to the new value
if key == "value": #If the attribute being changed is the value attribute:
if val in self.onEquals: #If the new value is something that should cause something to happen:
if type(self.onEquals[val]) == str: #If there's only one command:
exec(self.onEquals[val]) #execute it
elif type(self.onEquals[val]) == tuple: #If there's a tuple of commands:
for eachFunction in self.onEquals[value]: #Execute each of them
exec(eachFunction)它将被实例化如下:
foo = SharedAttribute(0, onEquals = {1: 'doSomething(arguments)', 2: ('doAnotherThing()', 'yetAnotherThing(arguments)'})当我尝试创建类的一个实例时,它给出了
AttributeError: 'SharedAttribute' object has no attribute 'onEquals'.我认为这是因为if val in self.onEquals在定义onEquals时试图访问onEquals,因为它调用__setattr__来定义它,但是我添加了if key == "value":,它仍然在运行。怎么了?
发布于 2015-11-17 22:47:58
您的问题是,__setattr__在__init__中也被调用为self.value = value,然后访问未定义的self.onEquals。
您可以尝试按建议更改self.value和self.onEquals行在__init__中的顺序。
但是,如果您不希望在实例化类时从onEquals运行函数,请保持原样,并检查onEquals是否已经在__setattr__中设置为:
def __setattr__(self, key, val):
super(SharedAttribute, self).__setattr__(key, val)
if key == "value" and getattr(self, "onEquals", False):
...发布于 2015-11-17 22:43:14
每次访问都会调用__setattr__来保存属性。当SharedAttribute的__init__()运行时,self.value和self.onEquals都不存在,但是__setattr__在设置self.value时会尝试访问self.onEquals --因为首先设置的是self.value。
你有几种不同的方法来解决这个问题:
__init__函数中的两行,或onEquals设置为空dict第一个解决方案是最快速和最简单的;第二个解决方案有一些风险,您可能会无意中访问onEquals的类版本,然后很难找到bug。
https://stackoverflow.com/questions/33768460
复制相似问题