我正在研究一个具有许多小功能的模块,但其docstring往往很长。这些docstrings使模块上的工作变得刺激了,因为我必须不断滚动长长的docstring才能找到一些实际的代码。

有没有办法使Docstrings与它们记录的功能分开?我真的很想能够在文件末尾指定远离代码的DOCSTRINGS,甚至更好地在单独的文件中指定docstrings。

有帮助吗?

解决方案

功能的Docstring可作为特殊属性提供 __doc__.

>>> def f(x):
...     "return the square of x"
...     return x * x
>>> f.__doc__
'return the square of x'
>>> help(f)
(help page with appropriate docstring)
>>> f.__doc__ = "Return the argument squared"
>>> help(f)
(help page with new docstring)

无论如何,这证明了这项技术。实际上,您可以:

def f(x):
    return x * x

f.__doc__ = """
Return the square of the function argument.

Arguments: x - number to square

Return value: x squared

Exceptions: none

Global variables used: none

Side effects: none

Limitations: none
"""

...或您想放入docstrings的任何东西。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top