Translate-CS 板


LINE

原文網址:http://google-styleguide.googlecode.com/svn/trunk/pyguide.html 註解 確認模組、函式、方法、行內註解的正確用法。 文件註解 (doc string) Python 有一個獨特的文件註解格式。文件註解是寫在 package, module, class, 或 function 開頭的一段字串,這個字串可用物件的 __doc__ member method 讀取,pydoc 也是使用這段文字 (試試對你寫的模組執行 pydoc 看會如何)。 使用文件註解的慣例是用三個雙引號來包住字串。一個文件註解的開頭要有 一行摘要,摘要的結束符號應該是句號、問號,或驚嘆號。摘要的下一行是空白行 。空白行的下一行的第一個字元應對齊摘要行的第一個雙引號。下面有更多文件 註解應遵守的格式。 模組 (module) 每一個檔案都應包含固定的 license 範本。選擇一個適合專案的範本 (例如: Apache 2.0, BSD, LGPL, GPL) 函式 (function) 與方法 (method) 本段落中所指的函式,泛指方法 (method),函式 (function),及生成器 (generator)。 除非以下的幾個狀況 *全部* 符合,否則每個函式都必須有文件註解。 1. 不會在模組外被呼叫或使用 2. 非常短 3. 很直觀 一個文件註解應該要讓使用者只看文件而不需要看程式碼就知道該如何呼叫該函 式。故文件註解應包含函式的呼叫語法及函式的用途,而非函式實現的細節。對 於比較複雜不直觀的程式,註解與程式碼放在一起要比放在文件註解中合適。 函式中的某些部份要被放在特定的段落中,以下一一列出。每個段落的段頭是一 個標頭行,標頭行以冒號結束。除了標頭行,其他的部份應使用兩個空白字元縮 排。 Args: 列出每個參數名,參數名後加上冒號,空一格,然後是該參數的敘述。若敘述太 長,一行 (80 個字元) 容納不下,下一行應縮排二或四個空白,整個文件的縮 排方式應一致。敘述應包含參數的類型 (type) 及用途。 若一個函式的參數接受 *foo (可變長度參數序列 variable length argument list) 或 **bar(任意關鍵字參數 arbitrary keyword argument),文件註解中應 列為 *foo 及 **bar。 Returns: (若是生成函式則用 Yields:) 敘述回傳值的資料型態 (type) 及其意義與用途。若函式回傳 None,則不一定需 要有這個段落。 Raises: 列出所有此介面的例外。 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. """ pass 類別 (class) 在類別定義底下應包含此類別的文件註解。若類別包含公開屬性 (public attribute),則應寫在 attribute 段落中,並遵守函式中 Arg 段落中的格式。 class SampleClass(object): """Summary of class here. Longer class information.... Longer class information.... Attributes: likes_spam: A boolean indicating if we like SPAM or not. eggs: An integer count of the eggs we have laid. """ def __init__(self, likes_spam=False): """Inits SampleClass with blah.""" self.likes_spam = likes_spam self.eggs = 0 def public_method(self): """Performs operation blah.""" 多行註釋及行註釋 最後一個該有註釋的地方是文件中較有技巧性的程式碼。若一段程式在下次的 code review 需要解釋,則應該要有註解。若需要多行註解來解釋較複雜的操作, 註釋應放在程式碼開始前。較不直觀的程式在行末應加上註解。 # We use a weighted dictionary search to find out where i is in # the array. We extrapolate position based on the largest num # in the array and the array size and then do binary search to # get the exact number. if i & (i-1) == 0: # true iff i is a power of 2 為了可讀性,註解應至少離程式碼兩個空白以上。 另一方面,絕不要敘述程式碼。假設閱讀該段程式碼的人比你更懂 Python (雖然 不一定懂你要做什麼)。 # BAD COMMENT: Now go through the b array and make sure whenever i occurs # the next element is i+1 --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 68.232.121.105 ※ 編輯: sandwichC 來自: 68.232.121.105 (05/14 08:32)







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP