4

我正在开发一个具有许多小功能但其文档字符串往往很长的模块。文档字符串使模块的工作变得烦人,因为我必须不断地滚动一个长文档字符串才能找到一点实际代码。

有没有办法将文档字符串与它们记录的函数分开?我真的希望能够在远离代码的文件末尾指定文档字符串,或者更好的是,在单独的文件中。

4

1 回答 1

12

函数的文档字符串可用作特殊属性__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
"""

...或任何你想放在你的文档字符串中的东西。

于 2011-01-19T07:48:17.370 回答