-1
我正在通過「Quick Python Book」第二版學習Python的對象。我正在使用Python 3Python3 @ Property.setter和Object沒有任何屬性
我想了解@property以及屬性的setter。 從第199頁CHPT 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