在 Google 风格的 Python 文档字符串中,可以指定Args
, Returns
, Raises
如下。
"""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
.
这样做的最佳方法是什么?
对于版本sphinx>=1.8.2
,你可以有一个自定义部分.
In your 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))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)