【发布时间】:2013-04-22 16:22:37
【问题描述】:
我正在通过“The Quick Python Book”第二版学习使用 Python 的对象。我正在使用 Python 3
我正在尝试了解@property 以及该属性的设置器。 从第 199 页第 15 页开始,我尝试了这个示例,但出现错误:
>>> class Temparature:
def __init__(self):
self._temp_fahr = 0
@property
def temp(self):
return (self._temp_fahr - 32) * 5/9
@temp.setter
def temp(self, new_temp):
self._temp_fahr = new_temp * 9 / 5 + 32
>>> t.temp
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
t.temp
AttributeError: 'Temparature' object has no attribute 'temp'
>>>
为什么会出现此错误?另外,为什么我不能只使用函数调用和参数设置实例变量 new_temp:
t = Temparature()
t.temp(34)
而不是
t.temp = 43
【问题讨论】:
标签: python object properties python-3.x