我有這樣一個類:Python中的財產返還財產對象
class Foo(object):
def __init__(self):
self.bar = property(self.get_bar)
def get_bar(self):
return "bar"
print Foo().bar #this prints <property object at 0x0051CB40>
我見過How do Python properties work?,How to set a python property in __init__,但它們都使用裝飾方法,我不因爲我希望有一個不同的名稱。我需要訪問self
我如何獲取屬性的行爲?
爲什麼要使用不同的名稱?有沒有合理的理由? –
你有沒有嘗試過使用屬性作爲裝飾或與文檔字符串? –
快速解決方案將顯式調用'bar'上的'__get__':'f = Foo(); f.bar .__ get __(f,type(f))' –