python - 为什么 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 不起作用了!

我错过了什么?

最佳答案

您似乎在使用 classic old-style classes在python 2中。为了properties要正常工作,您需要使用 new-style classes相反(在 python 2 中,您必须 inherit from 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
>>> 

另一个可能导致问题的细节是两个方法需要相同的名称才能使属性起作用。 如果你用这样的不同名称定义 setter,它将不起作用:

@x.setter
def x_setter(self, value):
    ...

还有一件一开始并不完全容易发现的事情是顺序:getter 必须首先定义。如果你先定义setter,你会得到name 'x' is not defined错误。

https://stackoverflow.com/questions/598077/

相关文章:

c - Linux内核如何编译自己?

python - 如何在 Python 中将 RGB 图像转换为灰度图像?

linux - 如何将后台应用程序的输出重定向到/dev/null

python - 从python pandas中的列名获取列索引

python - 在 Python 中记录未捕获的异常

linux - tcpdump:本地主机到本地主机

linux - 让 bash 脚本回答交互式提示

python - 使用 Python 计算目录的大小?

python - 如何在 Xvfb 中运行 Selenium?

python - TypeError : sequence item 0: expected str