看板 Python 關於我們 聯絡資訊
原文網址:http://google-styleguide.googlecode.com/svn/trunk/pyguide.html Python Style Rules *Semicolons 不要使用分號作為每行結尾 也不要使用分號將兩個指令塞進同一行 *Line length 每行最大長度為80字元 例外: 1.較長的 import 語句 2.註解中的URL 不要用反斜槓("\")來繼續句子 使用Python隱含的連接句子方式:"()","[]","{}"。 如果需要的話,可加入額外的括號。 Yes: foo_bar(self, width, height, color='black', design=None, x='foo', emphasis=None, highlight=0) if (width == 0 and height == 0 and color == 'red' and emphasis == 'strong'): 如果一個字串無法被寫為一行,使用"()"來連接 x = ('This will build a very long long ' 'long long long long long long string') 註解時,如遇需求,將長的URL放在同一行。 Yes: # See details at # http://www.example.com/us/developer/documentation/api/content/v2.0/csv_file_name_extension_full_specification.html No: # See details at # http://www.example.com/us/developer/documentation/api/content/\ # v2.0/csv_file_name_extension_full_specification.html 注意以上有連接句子時縮排的格式;可查看本指南關於縮排的說明。 *Parentheses 節儉地使用括號 不要使用"()"包住 回傳 或 條件 的語句,除非作為連接句子之用(參考上一點)。 但可用於包住tuples。 Yes: if foo: bar() while x: x = bar() if x and y: bar() if not x: bar() return foo for (x, y) in dict.items(): ... No: if (x): bar() if not(x): bar() return (foo) *Indentation 用四個空白來縮排你的程式碼。 不要用 tab 或是 tab混空白鍵 來縮排你的程式碼 遇到應該連接在一起的句子時,可選擇使用 垂直對齊,如同*line length(此指南第21點)的例子一般 四格空白的hanging indent(參考下例),用於第一行沒有引數的情況時。 Yes: # Aligned with opening delimiter foo = long_function_name(var_one, var_two, var_three, var_four) # 4-space hanging indent; nothing on first line foo = long_function_name( var_one, var_two, var_three, var_four) No: # Stuff on first line forbidden foo = long_function_name(var_one, var_two, var_three, var_four) # 2-space hanging indent forbidden foo = long_function_name( var_one, var_two, var_three, var_four) *Blank Lines top-level definitions之間:空兩行 method definitions之間:空一行 在函式或是類別的定義之間空兩行 方法(method)之間 、 class宣告行與第一個方法之間,都是空一行 Use single blank lines as you judge appropriate within functions or methods. #看不太懂這句,求解。 *Whitespace 遵守一般的方式在標點符號附近使用空白。 在"()","[]","{}"內不用空白 Yes: spam(ham[1], {eggs: 2}, []) No: spam( ham[ 1 ], { eggs: 2 }, [ ] ) 在"," , ";" , ":"後加入一個空白,前面不需要。 Yes: if x == 4: print x, y x, y = y, x No: if x == 4 : print x , y x , y = y , x 參數列、索引值、切片時用的"()","[]"前不應有空白。 Yes: spam(1) No: spam (1) Yes: dict['key'] = list[index] No: dict ['key'] = list [index] 在二元運算的兩端都加上一個空白:賦值("="),比較("==","<",">","!=","<>","<=", ">=","in","not in","is","is not"),布林運算子("and","or","not") 用你的判斷在算術符號間加入空白,但是永遠記得在二元運算的兩端都要同時加上空白。 Yes: x == 1 No: x<1 表達keyword argument或default parameter value時所用的"="周圍不要使用空白 Yes: def complex(real, imag=0.0): return magic(r=real, i=imag) No: def complex(real, imag = 0.0): return magic(r = real, i = imag) 不要使用空白達到連續幾行的垂直對齊效果,這會造成維護上的困難。 (適用於:":","#"...etc) Yes: foo = 1000 # comment long_name = 2 # comment that should not be aligned dictionary = { 'foo': 1, 'long_name': 2, } No: foo = 1000 # comment long_name = 2 # comment that should not be aligned dictionary = { 'foo' : 1, 'long_name': 2, } *Shebang Line 大部分的 .py 檔不須以 #! 為開頭。 以 #!/usr/bin/python 作為 main file 的開頭。 本行是用來讓kernel找到Python的直譯器,但在導入模組(importing modules)時會被忽略 因此,只有會被直接執行的檔案需要這行。 === 今天的都短短的,不知不覺就翻了好多點 下一個好長阿~~~ 容許我偷懶一下 Orz -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.30.143 ※ 編輯: tiefblau 來自: 140.112.30.143 (05/13 17:31)
darkgerm:Use single blank lines 那句 05/14 00:57
darkgerm:應該是說:在一個 method 或 function 內 05/14 00:58
darkgerm:如果有一些邏輯上的區塊,就用一個空行隔開 05/14 00:58