在表达式和语句中的空格 (Whitespace in Expressions and Statements)
宠物的烦恼 (Pet Peeves)
(注:即无伤大雅的小问题)
避免在下述情形中使用无关的空格:
- 紧挨着圆括号、方括号和花括号:
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)
- 紧贴在索引或切片 (indexing or slicing) 开始的开式括号前:
Yes: dict['key'] = list[index]
No: dict ['key'] = list [index]
- 在赋值 (或其他) 运算符周围的用于和其他语句对齐的一个以上的空格:
Yes:
| x = 1 | | y = 2 | | long_variable = 3COPY |
No:
| x = 1 | | y = 2 | | long_variable = 3COPY |
其他建议 (Other Recommendations)
- 始终在这些二元运算符两边放置一个空格:
assignment (=), augmented assignment (+=, -= etc.),
comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not),
Booleans (and, or, not).
- 在数学运算符两边使用空格:
Yes:
| i = i + 1 | | submitted += 1 | | x = x * 2 - 1 | | hypot2 = x * x + y * y | | c = (a + b) * (a - b)COPY |
No:
| i=i+1 | | submitted +=1 | | x = x*2 - 1 | | hypot2 = x*x + y*y | | c = (a+b) * (a-b)COPY |
- 不要在用于指定关键字参数 (keyword argument) 或默认参数值的 '=' 号周围使用
空格。
Yes:
| def complex(real, imag=0.0): | | return magic(r=real, i=imag)COPY |
No:
| def complex(real, imag = 0.0): | | return magic(r = real, i = imag)COPY |
- 复合语句 (Compound statements) (多条语句写在同一行) 一般不推荐。
Yes:
| if foo == 'blah': | | do_blah_thing() | | do_one() | | do_two() | | do_three()COPY |
Rather not:
| if foo == 'blah': do_blah_thing() | | do_one(); do_two(); do_three()COPY |
- 虽然有时可以在 if/for/while 的同一行跟一小段代码,但绝不要对多条子句
(multi-clause statements) 也这样做。也避免折叠这样的长行。
最好不要 (Rather not):
| if foo == 'blah': do_blah_thing() | | for x in lst: total += x | | while t < 10: t = delay()COPY |
明确不要 (Definitely not):
| if foo == 'blah': do_blah_thing() | | else: do_non_blah_thing() | | | | try: something() | | finally: cleanup() | | | | do_one(); do_two(); do_three(long, argument, | | list, like, this) | | | | if foo == 'blah': one(); two(); three()COPY |
注释 (Comments)
同代码不一致的注释比没注释更差。当代码修改时,始终优先更新注释!
注释应该是完整的句子。如果注释是一个短语或句子,首字母应该大写,除非它是一
个以小写字母开头的标识符 (永远不要修改标识符的大小写)。
如果注释很短,可以省略末尾的句号。注释块通常由一个或多个段落组成,段落是由
完整的句子构成的,每个句子应该以句号结尾。
你应该在结束语句的句点 (a sentence-ending period) 后使用两个空格。
用英语书写时,断词和空格是可用的 (When writing English, Strunk and White
apply)。
非英语国家的 Python 程序员:请用英语书写你的注释,除非你 120% 的确信代码永
远不会被不懂你的语言的人阅读。
注释块 (Block Comments)
注释块通常应用于跟随其后的一些 (或者全部) 代码,并和这些代码有着相同的缩进
层次。注释块中每行以 '#' 和一个空格开始 (除非它是注释内的缩进文本)。
注释块内的段落以仅含单个 '#' 的行分割。
行内注释 (Inline Comments)
节俭使用行内注释。
一个行内注释是和语句在同一行的注释。行内注释应该至少用两个空格和语句分开。
它们应该以一个 '#' 和单个空格开始。
行内注释不是必需的,事实上,如果说的是显而易见事,还会使人分心。不要这样做
:
x = x + 1 COPY
但是有时,这样是有益的:
x = x + 1 COPY
文档字符串 (Documentation Strings)
书写好的文档字符串 (又名"docstrings") 的约定,在 PEP 257 [3] 中是永存的。
- 为所有公共模块、函数、类和方法书写文档字符串。文档字符串对非公开的方法不
是必要的,但你应该有一条注释来描述这个方法做什么;这条注释应该出现在
"def" 行之后。
- PEP 257 描述了好的文档字符串约定。一定注意,多行文档字符串结尾的 """ 应该
单独成行,并推荐在其前加一空行,例如:
| """Return a foobang | | | | Optional plotz says to frobnicate the bizbaz first. | | | | """COPY |
- 对单行的文档字符串,结尾的 """ 在同一行也可以。
版本注记 (Version Bookkeeping)
如果你必须把 Subversion、CVS、or RCS crud 包含在你的源文件中,按如下做:
| __version__ = "$Revision: 54700 $" | | COPY |
这些行应该包含在模块的文档字符串之后,任何其他代码之前,上下各用一个空行分
隔 。 |