python - 文档字符串 - 一行与多行

标签 python documentation docstring

我正在将一些 ( epydoc ) 文档添加到我编写的包中,并且我遇到了很多重复自己多次的实例。

def script_running(self, script):
    """Return if script is running

    @param script: Script to check whether running

    @return: B{True} if script is running, B{False} otherwise
    @rtype: C{bool}
    """

PEP257说:

One-liners are for really obvious cases.

还有

The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable).


是否有关于何时在单行(描述)和完整参数/返回字段之间划清界限的一般准则或标准做法?

或者在生成文档时,我应该包括每个功能的每个适用字段,不管它看起来有多么重复?

奖励问题:从语法上讲,描述 script 参数的最佳方式是什么?

最佳答案

您正在寻找的一般指南就在 PEP257 中。在您引用的内容中,也许您只需要实际操作即可。

您的函数非常适合单行文档字符串(“非常明显的案例”):

def script_running(self, script):
    """Check if the script is running."""

通常,如果您说一个函数正在检查某些内容,则意味着它将返回 TrueFalse,但如果您愿意,可以更具体一些:

def script_running(self, script):
    """Return True if the script is running, False otherwise."""

再次全部在一行中。

我可能还会更改函数的名称,因为没有必要强调函数在其名称(脚本)中的作用。函数名应该是甜美的、简短的并且对函数的作用有意义。可能我会选择:

def check_running(self, script):
    """Return True if the script is running, False otherwise."""

有时 function-name-imagination 厌倦了所有的编码,但无论如何您都应该尽力而为。

对于多行示例,让我从 google guidelines 中借用一个文档字符串:

def fetch_bigtable_rows(big_table, keys, other_silly_variable=None):
    """Fetches rows from a Bigtable.

    Retrieves rows pertaining to the given keys from the Table instance
    represented by big_table.  Silly things may happen if
    other_silly_variable is not None.

    Args:
        big_table: An open Bigtable Table instance.
        keys: A sequence of strings representing the key of each table row
            to fetch.
        other_silly_variable: Another optional variable, that has a much
            longer name than the other args, and which does nothing.

    Returns:
        A dict mapping keys to the corresponding table row data
        fetched. Each row is represented as a tuple of strings. For
        example:

        {'Serak': ('Rigel VII', 'Preparer'),
         'Zim': ('Irk', 'Invader'),
         'Lrrr': ('Omicron Persei 8', 'Emperor')}

        If a key from the keys argument is missing from the dictionary,
        then that row was not found in the table.

    Raises:
        IOError: An error occurred accessing the bigtable.Table object.
    """

这可能是“总结其行为并记录其参数、返回值、副作用、引发的异常以及调用时间限制(如果适用)”的一种方式.

您可能也有兴趣查看此 example of pypi project它应该用 Sphinx 记录.

我的 2 美分:指南旨在让您了解您应该做什么和不应该做什么,但它们不是严格的规则,您必须盲目执行跟随。所以最后选择你觉得更好的。


我想澄清一下另一个答案中提到的关于使用文档字符串达到最大行长度的内容。

PEP8告诉您“将所有行限制为最多 79 个字符”,即使最后每个人都做了 80 个字符。

这是 80 个字符:

--------------------------------------------------------------------------------

这可能是一种边缘情况,您真正需要的只是一个有点长的句子:

def my_long_doc_function(arg1, arg2):
    """This docstring is long, it's a little looonger than the 80 characters
    limit.
    
    """

就像一个单行文档字符串,这意味着用于非常明显的情况,但在您的编辑器上(有 80 个字符的限制)是多行的。

关于python - 文档字符串 - 一行与多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9392096/

相关文章:

python - B 和 C 不工作(Python3)

python - H2O服务器错误 : Cluster reports unhealthy status

python - Sphinx在make html之后找不到我的任何模块

python - 如何从函数本身内部打印python函数的Docstring?

python - vscode 文档字符串未正确显示

python - 惯用的异步设计

python - search_field 按字段选择

c++ - 重命名文件路径和 Doxygen

javascript - 自动生成用于JavaScript库的Visual Studio VsDoc

Python Visual Studio Code autoDocstring 配置