2012-02-22 30 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的禪宗。