Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取属性的doc string属性?_Python_Python 3.x_Properties_Descriptor - Fatal编程技术网

Python 如何获取属性的doc string属性?

Python 如何获取属性的doc string属性?,python,python-3.x,properties,descriptor,Python,Python 3.x,Properties,Descriptor,在学习Python描述符时,我遇到了这个示例 class Person(object): def __init__(self): self._name = '' def fget(self): print("Getting: %s" % self._name) return self._name def fset(self, name): print("Setting: %s" % name)

在学习Python描述符时,我遇到了这个示例

class Person(object):
    def __init__(self):
        self._name = ''

    def fget(self):
        print("Getting: %s" % self._name)
        return self._name

    def fset(self, name):
        print("Setting: %s" % name)
        self._name = name.title()

    def fdel(self):
        print("Deleting: %s" %self._name)
        del self._name

    name = property(fget, fset, fdel, "I'm the property.")
它使用
属性
函数。第四个参数是doc–docstring

但当我试图去看医生时,它会引起

AttributeError: 'str' object has no attribute 'doc'

首先,因为doc字符串是一个神奇的属性,所以它应该是一个表单。所以,它是
\uuuuu doc\uuuu
而不是
doc

其次,当您尝试从类的实例访问
\uuuu doc\uuuu
时,它将触发实际对象的doc属性,在本例中,它是一个字符串。相反,请尝试从类对象访问属性:

In [74]: Person.name.__doc__
Out[74]: "I'm the property."
In [74]: Person.name.__doc__
Out[74]: "I'm the property."