我试图用新的样式属性声明:“无法设置属性”与新样式属性在Python
class C(object):
def __init__(self):
self._x = 0
@property
def x(self):
print 'getting'
return self._x
@x.setter
def set_x(self, value):
print 'setting'
self._x = value
if __name__ == '__main__':
c = C()
print c.x
c.x = 10
print c.x
并查看控制台以下:
pydev debugger: starting
getting
0
File "\test.py", line 55, in <module>
c.x = 10
AttributeError: can't set attribute
我究竟做错了什么? P.S .:旧式宣言正常。
我已经回滚了将'setting'(一个文档字符串)变成'print'setting'(一个简单的调试语句)的编辑。虽然打印语句的意图确实是合理的,但文档字符串中没有任何伤害或错误,它根本不会影响问题。 – 2016-01-18 22:10:40