我在编写的包中添加了一些(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说:
一句话适用于非常明显的情况。
以及
函数或方法的文档字符串应总结其行为,并记录其参数、返回值、副作用、引发的异常以及对何时可以调用它的限制(如果适用,请全部记录)。
对于何时在单行(描述)和完整参数/返回字段之间划线,是否有通用指南或标准实践?
或者,在生成文档时,我应该包括每个函数的每个适用字段,而不管它看起来有多重复?
额外的问题:从语法上讲,描述script
参数的最佳方式是什么?
您正在寻找的通用指南在您引用的PEP257中是正确的,也许您只需要看看它的实际操作。
您的函数是单行文档字符串的一个很好的候选者("非常明显的情况"):
def script_running(self, script):
"""Check if the script is running."""
通常,如果你说一个函数正在检查某个东西,这意味着它将返回True
或False
,但如果你愿意,你可以更具体地说:
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."""
有时函数名的想象力被所有的编码所累,但无论如何你都应该尽力。
对于一个多行示例,让我借用谷歌指南中的一个文档字符串:
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.
"""
这可能是"的一种方式;总结它的行为并记录它的参数、返回值、副作用、引发的异常以及对何时可以调用它的限制(如果适用的话)。
您可能也有兴趣看看这个pypi项目的例子,它将与Sphinx一起记录。
我的2美分:准则旨在让你知道你应该做什么和不应该做什么,但它们不是你必须盲目遵循的严格规则。所以最后选择你觉得更好的。
我想澄清一下另一个答案中所说的关于用docstring点击最大行长度的内容。
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个字符)是多行的。
我认为在为文档字符串添加扩展语法时,可能总是会有一定程度的重复,即epydoc/sphinx标记。
我还想说,这件事是主观的,而不是客观的。显式比隐式更好,而且似乎更遵循Python的禅宗。