Python 如何获取属性的doc string属性?
在学习Python描述符时,我遇到了这个示例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)
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."