簡體   English   中英

有沒有辦法讓文檔字符串與它們記錄的函數分開?

[英]Is there a way to keep docstrings separate from the functions they document?

我正在研究一個具有許多小函數的模塊,但其文檔字符串往往很長。 文檔字符串使得模塊工作變得煩人,因為我必須不斷滾動一個長文檔字符串來查找一些實際代碼。

有沒有辦法讓文檔字符串與它們記錄的函數分開? 我真的希望能夠在遠離代碼的文件末尾指定文檔字符串,或者甚至更好地在單獨的文件中指定。

函數的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
"""

......或者你想要放在文檔字符串中的任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM