-3
我在Python2.7中編寫了一個簡單的類,它應該使用@property
功能。Python 2.7 @屬性用法導致錯誤「全局名稱_c__pro'未定義」
class c():
def __init__(self):
__pro = 1
@property
def pro(self):
return __pro *10
def setpro(self, x):
__pro = x
現在,當我從這個類來創建一個對象,並嘗試訪問pro
財產,我得到以下錯誤:
>>> x = c()
>>> x.pro
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in pro
NameError: global name '_c__pro' is not defined
注意,整個事情是相同的蟒蛇commandline-裏面寫會話,所以它應該與丟失導入或錯誤的導入命名空間無關。
我在這裏做錯了什麼?我如何重寫它以訪問pro
?
實際上使其成爲*產權*'上的self'而不是一個局部變量...!? – deceze