0
我使用的是类装饰,但我不明白与SETATTR如何设置属性,这是我的代码:__setattr__类装饰蟒蛇
def cldecor(*par):
def onDecorator(aClass):
class wrapper:
def __init__(self, *args):
self.wrapped = aClass(*args)
def __getattr__(self, name):
return getattr(self.wrapped, name)
def __setattr__(self, attribute, value):
if attribute == 'wrapped':
self.__dict__[attribute] = value
else:
setattr(self.wrapped, attribute, value)
return wrapper
return onDecorator
@cldecor('data','size')
class Doubler:
def __init__(self,label,start):
self.label = label
self.data = start
def display(self):
print('{0} => {1}'.format(self.label, self.data))
但是当我做:
if __name__ == "__main__":
X = Doubler('X is ', [1,2,3])
X.xxx = [3,4,9]
print(X.xxx)
X.display()
我有这样的输出:
[3, 4, 9]
X is => [1, 2, 3]
我怎么可以有这样的输出呢?
[3, 4, 9]
X is => [3, 4, 9]
也许解释你认为这应该达到什么? – Marcin 2012-02-23 14:40:33
* * par在你的代码中做了什么?您将其保留未使用... – glglgl 2012-02-23 14:43:50
您发布的代码不会生成您显示的输出。我得到的输出是''[3,4,9] \ n X is => [1,2,3]'' – AdamKG 2012-02-23 14:46:50