属性的教条

Dha*_*ara 3 python python-2.7

如何访问属性的文档字符串而不是它所拥有的值?

为什么以下代码中的2个帮助函数会返回不同的文档字符串abc.x

class C(object):
    def __init__(self):
        self._x = None

    def getx(self):
        print "** In get **"
        return self._x

    x = property(getx, doc="I'm the 'x' property.")

abc = C()
help(abc) # prints the docstring specified for property 'x'
help(abc.x) # prints the docstring for "None", the value of the property
Run Code Online (Sandbox Code Playgroud)

cmd*_*cmd 7

发生这种情况因为abc.x已经解决了None.然后None被传递给help().试试这个:

help(C.x)
Run Code Online (Sandbox Code Playgroud)