为什么Python中的@ foo.setter对我不起作用?
问题内容:
因此,我正在使用Python 2.6中的装饰器,并且在使它们工作时遇到了一些麻烦。这是我的课程文件:
class testDec:
@property
def x(self):
print 'called getter'
return self._x
@x.setter
def x(self, value):
print 'called setter'
self._x = value
我认为这意味着将其视为x
属性,但是在get和set上调用这些函数。因此,我启动了IDLE并进行了检查:
>>> from testDec import testDec
from testDec import testDec
>>> t = testDec()
t = testDec()
>>> t.x
t.x
called getter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "testDec.py", line 18, in x
return self._x
AttributeError: testDec instance has no attribute '_x'
>>> t.x = 5
t.x = 5
>>> t.x
t.x
5
显然,第一次调用按预期方式工作,因为我调用了getter,并且没有默认值,并且失败。好,很好,我了解。但是,对assign的调用t.x = 5
似乎会创建一个新属性x
,现在getter无效!
我想念什么?
问题答案:
您似乎在python 2中使用经典的老式类。为了使属性正常工作,您需要使用新型类(在python
2中,您必须继承自object
)。只需将您的课程声明为MyClass(object)
:
class testDec(object):
@property
def x(self):
print 'called getter'
return self._x
@x.setter
def x(self, value):
print 'called setter'
self._x = value
有用:
>>> k = testDec()
>>> k.x
called getter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/devel/class_test.py", line 6, in x
return self._x
AttributeError: 'testDec' object has no attribute '_x'
>>> k.x = 5
called setter
>>> k.x
called getter
5
>>>
可能导致问题的另一个细节是,这两种方法都需要相同的名称才能使该属性起作用。 如果您使用类似这样的其他名称来定义设置器,它将无法正常工作 :
@x.setter
def x_setter(self, value):
...
首先,还不完全容易发现的另一件事是顺序: 必须先定义 吸气剂。如果首先定义设置器,则会 name 'x' is not defined
出错。