您正在寻找的一般指导原则是在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."""
有时函数名,想象力是所有编码很累,但你应该尽量尝试做到最好。
对于多例子,让我从google guidelines借用文档字符串:
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.
"""
这可能是一个办法“总结自己的行为并记录它的参数,返回值(一个或多个),副作用例外情况,以及何时可以调用的限制(所有情况适用)“。
您可能也有兴趣看看这example of pypi project,它意味着与Sphinx记录。
我的2美分:准则是为了给你你应该和不应该做的事情的想法,但他们没有严格的规定,你必须盲从。所以最后选择你觉得更好的东西。
我想清楚了的东西,在约击中文档字符串的行的最大长度另一个答案已经说。
PEP8告诉你“限制所有线路为最多79个字符”即使在最后每个人都没有80
这是80个charachters:
--------------------------------------------------------------------------------
,这可能是一个边长的情况,你需要一点点长句子:
def my_long_doc_function(arg1, arg2):
"""This docstring is long, it's a little looonger than the 80 charachters
limit.
"""
就像一条线docstring,意思是对于真正明显的情况,但是在你的编辑器(80字符限制)上有多行。