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

请输入看板名称,例如:Soft_Job站内搜寻

TOP