如何使用Google文档字符串样式设置长行的参数说明

这是Google样式文档字符串的摘录:

def function_with_types_in_docstring(param1,param2):     “”“示例函数,其文档字符串中记录了类型。

`PEP 484`_ type annotations are supported. If attribute,parameter,and
return types are annotated according to `PEP 484`_,they do not need to be
included in the docstring:

Args:
    param1 (int): The first parameter.
    param2 (str): The second parameter.

Returns:
    bool: The return value. True for success,False otherwise.

.. _PEP 484:
    https://www.python.org/dev/peps/pep-0484/

"""

如果描述跨越一行,如何格式化args?

hyx_hyx_hyx 回答:如何使用Google文档字符串样式设置长行的参数说明

当描述跨越多行时,您可以折行并缩进。在您的示例中:

"""
`PEP 484`_ type annotations are supported. If attribute,parameter,and
return types are annotated according to `PEP 484`_,they do not need to be
included in the docstring:

Args:
    param1 (int): A really long description of the first parameter that
        spans more than one line. If you've got a really long description
        you can continue to break the line where you want,indent and 
        continue describing your parameter
    param2 (str): The second parameter.

Returns:
    bool: The return value. True for success,False otherwise.

.. _PEP 484:
    https://www.python.org/dev/peps/pep-0484/

"""

这可以使文档字符串保持可读性,并缩短行长。如果要导出到Sphinx,它也会保留格式。

这是Google Python Style Guide

中的推荐方法

按名称列出每个参数。名称后应加上描述,并用冒号和空格分隔。 如果描述太长而无法容纳单个80个字符的行,请使用2或4个空格的悬挂缩进(与文件的其余部分一致)。 如果代码不包含相应的类型注释,则说明中应包含所需的类型。

本文链接:https://www.f2er.com/3074872.html

大家都在问