2012-02-17 20 views
4
class MyClass(object): 
    def __init__(self): 
     self._my_secret_thing = 1 

    def _i_get(self): 
     return self._my_secret_thing 

    def _i_set(self, value): 
     self._my_secret_thing = value 

    def _i_delete(self): 
     del self._my_secret_thing 

    my_thing = property(_i_get, _i_set, _i_delete,'this document for my_thing') 

instance_of = MyClass() 

help(instance_of.my_thing) # not display the 'this document for my_thing' 
help(instance_of)   # display the 'this document for my_thing' 

問題>爲什麼對於my_thing幫助信息顯示不出來,如果它是通過help(instance_of.mything)稱爲所謂的財產文檔功能不顯示幫助信息?爲什麼當它是通過幫助(instance.attribute)

參考python.property

回答

6

當您訪問instance_of.my_thing,則返回值 - 還等什麼你實際上是呼籲help是價值1而不是屬性。

如果您在類對象而不是實例上訪問它,您將獲得屬性對象,並且文檔字符串將被附加到它;即使用help(MyClass.my_thing)help(type(instance_of).my_thing)