4
4
* [ Introduction - 简介] ( #1 )
5
5
* [ A Foolish Consistency is the Hobgoblin of Little Minds - 愚蠢的一致性是小心灵的大地精] ( #2 )
6
6
* [ Code lay-out - 代码布局] ( #3 )
7
- * [ Indentation - 缩进] ( #3.1 )
8
- * [ Tabs or Spaces? - A罩杯还是C罩杯?] ( #3.2 )
9
- * [ Maximum Line Length - 代码行最大长度] ( #3.3 )
10
- * [ Should a line break before or after a binary operator? - 在二元运算符之前还是之后断行?] ( #3.4 )
11
- * [ Blank Lines - 空行] ( #3.5 )
12
- * [ Source File Encoding - 源文件编码] ( #3.6 )
13
- * [ Imports - 导入] ( #3.7 )
14
- * [ Module level dunder names - 模块级 dunder(文中解释) 名称] ( #3.8 )
7
+ * [ Indentation - 缩进] ( #3.1 )
8
+ * [ Tabs or Spaces? - A罩杯还是C罩杯?] ( #3.2 )
9
+ * [ Maximum Line Length - 代码行最大长度] ( #3.3 )
10
+ * [ Should a line break before or after a binary operator? - 在二元运算符之前还是之后断行?] ( #3.4 )
11
+ * [ Blank Lines - 空行] ( #3.5 )
12
+ * [ Source File Encoding - 源文件编码] ( #3.6 )
13
+ * [ Imports - 导入] ( #3.7 )
14
+ * [ Module level dunder names - 模块级 dunder(文中解释) 名称] ( #3.8 )
15
15
* [ String Quotes - 字符串引号] ( #4 )
16
16
* [ Whitespace in Expressions and Statements - 表达式和语句中的空格] ( #5 )
17
- * [ Pet Peeves - 心理藏的小烦恼] ( #5.1 )
18
- * [ Other Recommendations - 其他建议] ( #5.2 )
17
+ * [ Pet Peeves - 心理藏的小烦恼] ( #5.1 )
18
+ * [ Other Recommendations - 其他建议] ( #5.2 )
19
19
* [ When to use trailing commas - 何时使用逗号结尾] ( #6 )
20
20
* [ Comments - 注释] ( #7 )
21
- * [ Block Comments - 块注释] ( #7.1 )
22
- * [ Inline Comments - 行注释] ( #7.2 )
23
- * [ Documentation Strings - 文档字符串] ( #7.3 )
21
+ * [ Block Comments - 块注释] ( #7.1 )
22
+ * [ Inline Comments - 行注释] ( #7.2 )
23
+ * [ Documentation Strings - 文档字符串] ( #7.3 )
24
24
* [ Naming Conventions - 命名约定] ( #8 )
25
- * [ Overriding Principle - 圣经戒律] ( #8.1 )
26
- * [ Descriptive: Naming Styles - 描述的: 命名样式] ( #8.2 )
27
- * [ Prescriptive: Naming Conventions - 规定的: 命名规定] ( #8.3 )
28
- * [ Names to Avoid - 避免的命名] ( #8.3.1 )
29
- * [ Package and Module Names - 包和模块名] ( #8.3.2 )
30
- * [ Class Names - 类名] ( #8.3.3 )
31
- * [ Type variable names - 类型变量名] ( #8.3.4 )
32
- * [ Exception Names - 异常名] ( #8.3.5 )
33
- * [ Global Variable Names - 全局变量名] ( #8.3.6 )
34
- * [ Function Names - 函数名] ( #8.3.7 )
35
- * [ Function and method arguments - 函数和方法参数] ( #8.3.8 )
36
- * [ Method Names and Instance Variables - 方法名和实例变量] ( #8.3.9 )
37
- * [ Constants - 常量] ( #8.3.10 )
38
- * [ Designing for inheritance - 继承的设计] ( #8.3.11 )
39
- * [ Public and internal interfaces - 公共和内部接口] ( #8.4 )
25
+ * [ Overriding Principle - 圣经戒律] ( #8.1 )
26
+ * [ Descriptive: Naming Styles - 描述的: 命名样式] ( #8.2 )
27
+ * [ Prescriptive: Naming Conventions - 规定的: 命名规定] ( #8.3 )
28
+ * [ Names to Avoid - 避免的命名] ( #8.3.1 )
29
+ * [ Package and Module Names - 包和模块名] ( #8.3.2 )
30
+ * [ Class Names - 类名] ( #8.3.3 )
31
+ * [ Type variable names - 类型变量名] ( #8.3.4 )
32
+ * [ Exception Names - 异常名] ( #8.3.5 )
33
+ * [ Global Variable Names - 全局变量名] ( #8.3.6 )
34
+ * [ Function Names - 函数名] ( #8.3.7 )
35
+ * [ Function and method arguments - 函数和方法参数] ( #8.3.8 )
36
+ * [ Method Names and Instance Variables - 方法名和实例变量] ( #8.3.9 )
37
+ * [ Constants - 常量] ( #8.3.10 )
38
+ * [ Designing for inheritance - 继承的设计] ( #8.3.11 )
39
+ * [ Public and internal interfaces - 公共和内部接口] ( #8.4 )
40
40
* [ Programming Recommendations - 编码建议] ( #9 )
41
- * [ Function Annotations - 函数注释] ( #9.1 )
41
+ * [ Function Annotations - 函数注释] ( #9.1 )
42
42
* [ References - 参考文献] ( #10 )
43
43
* [ Copyright - 版权] ( #11 )
44
44
53
53
<h5 id =" 3.1 " >缩进</h5 >
54
54
一个缩进级别四个空格。
55
55
* 连续行使用两种方式使封装元素成为一行:括号内垂直隐式连接和悬挂式缩进,使用悬挂式缩进应该注意第一行不应该有参数,连续行要使用进一步的缩进来区分。
56
+
56
57
``` Python
57
58
# Aligned with opening delimiter.
58
59
foo = long_function_name(var_one, var_two,
@@ -69,7 +70,9 @@ foo = long_function_name(
69
70
var_one, var_two,
70
71
var_three, var_four)
71
72
```
73
+
72
74
* 当 if 语句过长需要换行时,以下处理方法可以采用。
75
+
73
76
``` Python
74
77
# No extra indentation.
75
78
if (this_is_one_thing and
@@ -88,7 +91,9 @@ if (this_is_one_thing
88
91
and that_is_another_thing):
89
92
do_something()
90
93
```
94
+
91
95
* 当闭环括号内元素跨行时,可以采用以下方式。
96
+
92
97
``` Python
93
98
my_list = [
94
99
1 , 2 , 3 ,
@@ -99,6 +104,7 @@ result = some_function_that_takes_arguments(
99
104
' d' , ' e' , ' f' ,
100
105
)
101
106
```
107
+
102
108
<h5 id =" 3.2 " >A罩杯还是C罩杯?</h5 >
103
109
除非项目中已经约定了使用 tab 作为缩进,最好使用 space。
104
110
Python 3 不允许 tab 和 space 混用,同时混用了 tab 和 space 的 Python 2 代码应该被转换为仅使用 space。
@@ -107,14 +113,17 @@ Python 3 不允许 tab 和 space 混用,同时混用了 tab 和 space 的 Pyth
107
113
对于具有较少结构限制(文档字符串或注释)的长文本块,行长度应限制为72个字符。
108
114
当然了,不要问为啥非得是 79,72。我们要兼顾非洲大陆人民的生活。代码是全世界的。
109
115
反斜杠有时可能仍然要用。 例如,又多又长的 with - 语句不能使用隐式连接,这时反斜杠是可以接受的:
116
+
110
117
``` Python
111
118
with open (' /path/to/some/file/you/want/to/read' ) as file_1, \
112
119
open (' /path/to/some/file/being/written' , ' w' ) as file_2:
113
120
file_2.write(file_1.read())
114
121
```
122
+
115
123
assert 语句也是如此。
116
124
<h5 id =" 3.4 " >在二元运算符之前还是之后断行?</h5 >
117
125
算法和程序设计技术先驱,计算机排版系统 TEX 和 METAFONT 的发明者 Donald Knuth,推荐使用以下形式:
126
+
118
127
``` Python
119
128
# Yes: easy to match operators with operands
120
129
income = (gross_wages
@@ -123,6 +132,7 @@ income = (gross_wages
123
132
- ira_deduction
124
133
- student_loan_interest)
125
134
```
135
+
126
136
只要保持本地一致性,在二元运算符之前和之后断开都是允许的,但是新的 Python 代码推荐使用 Knuth 形式。
127
137
<h5 id =" 3.5 " >空行</h5 >
128
138
顶层函数和类定义使用两个空行。
@@ -135,48 +145,61 @@ Python 2 默认ASCII,Python 3 默认UTF-8。
135
145
源文件最好只使用 ASCII 字符,即使是蹩脚的 Chinglish 亦可,家和万事兴。
136
146
137
147
<h5 id =" 3.7 " >模块导入</h5 >
148
+
138
149
``` Python
139
150
YES : from subprocess import Popen, PIPE
140
151
import os
141
152
import sys
142
153
143
154
NO : import sys, os
144
155
```
156
+
145
157
模块导入总是位于文件顶部,在模块注释和文档字符串之后,模块全局变量和常量之前。
146
158
导入应该按照以下顺序分组,不同组间用空行隔离。
147
159
* 标准库 imports
148
160
* 相关第三方 imports
149
161
* 本地特定应用/库 imports
150
162
推荐使用绝对导入,标准库代码应总是使用绝对导入。
163
+
151
164
``` Python
152
165
import mypkg.sibling
153
166
from mypkg import sibling
154
167
from mypkg.sibling import example
155
168
```
169
+
156
170
在包结构比较复杂时,可以使用相对导入。
171
+
157
172
``` Python
158
173
from . import sibling
159
174
from .sibling import example
160
175
```
176
+
161
177
在 Python 3 中,相对导入已经被删除,禁止使用。
162
178
类导入:
179
+
163
180
``` Python
164
181
from myclass import MyClass
165
182
from foo.bar.yourclass import YourClass
166
183
```
184
+
167
185
如果这种方式导致了本地命名冲突,可以使用以下方式:
186
+
168
187
``` Python
169
188
import myclass
170
189
import foo.bar.yourclass
171
190
```
191
+
172
192
然后使用 myclass.MyClass 和 foo.bar.yourclass.YourClass。
173
193
请不要使用以下方式:
194
+
174
195
``` Python
175
196
from < module> import *
176
197
```
198
+
177
199
<h5 id =" 3.8 " >模块级别 dunder 名称</h5 >
178
200
模块级别 "dunders"(即具有两个前导和两个后缀下划线的名称),例如 \_\_ all\_\_ ,\_\_ author\_\_ ,\_\_ version\_\_ 等应放在模块 docstring 之后,但在任何 import 语句之前,但是除了 \_\_ future\_\_ 导入。 Python 强制 future-imports 必须在除了 docstrings 之外的任何其他代码之前出现在模块中。
179
201
例如:
202
+
180
203
``` Python
181
204
""" This is the example module.
182
205
@@ -192,6 +215,7 @@ __author__ = 'Cardinal Biggles'
192
215
import os
193
216
import sys
194
217
```
218
+
195
219
<h5 id =" 4 " >字符串引号</h5 >
196
220
在 Python 中,单引号和双引号是等价的,只需要坚持使用一种并保持一致即可。
197
221
在双引号中使用单引号,单引号中使用双引号。三引号中使用双引号。
0 commit comments