Python谷歌风格的DocString函数没有参数



我使用Google样式Python Docstring格式已经有一段时间了。我处理没有参数的函数/方法的方式突然看起来不正确了。我做了一些搜索,在网上找不到任何指定如何处理这种情况的内容。

当没有返回时,我看到使用了None,我对此没有意见,因为从技术上讲,就是返回的。然而,使用None作为参数可能意味着实际上有一个参数的类型应该是:NoneType

目前,我一直在做的事情看起来像这样:

def foo():
"""
blah blah blah
Args:
None
Returns:
The number 5
"""
return 5

我的问题是,我应该使用哪种格式(我更喜欢始终使用Args部分(?或者,也许我目前的做法并没有那么糟糕,而且是常见的做法。

其他一些候选人(如果你觉得有更好的格式,可以随意提供自己的(:

def foo():
"""
blah blah blah
Args:
Returns:
The number 5
"""
return 5
def foo():
"""
blah blah blah
Args:
No arguments
Returns:
The number 5
"""
return 5
def foo():
"""
blah blah blah
Returns:
The number 5
"""
return 5

在我看来,如果函数没有参数,那么标准是没有"Arguments"部分。与您列出的最终候选人风格相同。

这一点没有明确提及,但我从以下网站的例子中推断出:

  • https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
  • https://google.github.io/styleguide/pyguide.html

最新更新