Google样式的python文档字符串中的“写”?

时间:2019-04-18 13:51:56

标签: python python-sphinx

在Google样式的python文档字符串中,可以如下指定ArgsReturnsRaises

"""This is an example of Google style.

Args:
    param1: This is the first param.
    param2: This is a second param.

Returns:
    This is a description of what is returned.

Raises:
    KeyErr
"""

我有许多函数,它们不返回任何东西,而是将结果写入磁盘。我发现通常也可以记录该函数将写入磁盘的内容,例如使用Writes:sphinx.ext.napoleon似乎不支持。

这样做的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

对于版本sphinx>=1.8.2,您可以拥有custom section

在您的conf.py中,您应该添加选项napoleon_custom_sections = ('Writes', 'Parameters')(要使用参数创建别名)

然后您可以通过以下方式编写文档字符串:

from sphinxcontrib.napoleon import Config
from sphinxcontrib.napoleon import GoogleDocstring

config = Config(napoleon_use_param=True, napoleon_use_rtype=True, napoleon_custom_sections=('Writes', 'Parameters'))
docstring="""This is an example of Google style with a custom section.

Args:
    param1: This is the first param.
    param2: This is a second parpytham.

Returns:
    This is a description of what is returned.

Raises:
    KeyErr

Writes:
    write1: This is writting things !

"""

print(GoogleDocstring(docstring, config))