在Python属性上放置docstring的正确方法是什么?
我应该制作几个docstring,还是只制作一个(我应该把它放在哪里)在Python属性上放置docstring的正确方法是什么?,python,properties,decorator,Python,Properties,Decorator,我应该制作几个docstring,还是只制作一个(我应该把它放在哪里) 我看到property()接受一个doc参数。在getter上编写docstring,因为1)这就是help(MyClass)显示的内容,2)它也是在getter中完成的 关于(1): 然后: >>> c = C() >>> help(c) Help on C in module __main__ object: class C(__builtin__.object) | Data
我看到
property()
接受一个doc参数。在getter上编写docstring,因为1)这就是help(MyClass)
显示的内容,2)它也是在getter中完成的
关于(1):
然后:
>>> c = C()
>>> help(c)
Help on C in module __main__ object:
class C(__builtin__.object)
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| x
| Get x
>>>
注意,setter的docstring“Set x”被忽略
因此,您应该在getter函数上为整个属性(getter和setter)编写docstring,使其可见。一个好的属性docstring示例可能是:
class Serial(object):
@property
def baudrate(self):
"""Get or set the current baudrate. Setting the baudrate to a new value
will reconfigure the serial port automatically.
"""
return self._baudrate
@baudrate.setter
def baudrate(self, value):
if self._baudrate != value:
self._baudrate = value
self._reconfigure_port()
setter必须记录在getter中
>>> c = C()
>>> help(c)
Help on C in module __main__ object:
class C(__builtin__.object)
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| x
| Get x
>>>
class Serial(object):
@property
def baudrate(self):
"""Get or set the current baudrate. Setting the baudrate to a new value
will reconfigure the serial port automatically.
"""
return self._baudrate
@baudrate.setter
def baudrate(self, value):
if self._baudrate != value:
self._baudrate = value
self._reconfigure_port()