2012-02-22 82 views
8

我在我写的一个包中添加了一些(epydoc)文档,并且我遇到了许多我重复自己多次的实例。Docstrings - 一条线对多条线

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说:

一行程序是真正明显的情况下。

文档字符串函数或方法应该总结它的行为和记录它的参数,返回值(一个或多个),副作用小,引起的异常和限制时,它可以被称为(如果适用的话)。


是否有一个通用准则或标准做法时画一个班轮(介绍)和全PARAM /回报领域之间的界限?

或者当生成文档时,我应该为每个函数包含每个适用的字段,而不管它看起来有多重复?

红利问题:在句法上,描述script param的最佳方式是什么?

回答

16

您正在寻找的一般指导原则是在PEP257正确的引用内容,也许您只需要在行动中看到它。

你的功能是一个在线文档字符串一个很好的候选人(“真的很明显的情况下”):

def script_running(self, script): 
    """Check if the script is running.""" 

通常,如果你说一个功能是检查的东西就意味着它要返回TrueFalse,但如果你喜欢,你能更具体:

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字符限制)上有多行。

4

我认为在为docstrings添加扩展语法(即epydoc/sphinx标记)时,可能总是存在某种程度的重复。

我也想说这个问题比主观更主观。显式比隐式更好,似乎更多地遵循Python的禅宗。