Python 板


LINE

原文網址: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)
1F:推 darkgerm:Use single blank lines 那句 05/14 00:57
2F:→ darkgerm:應該是說:在一個 method 或 function 內 05/14 00:58
3F:→ darkgerm:如果有一些邏輯上的區塊,就用一個空行隔開 05/14 00:58







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燈, 水草

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

TOP