52
52
2、遵循此风格后和其他 python 版本不兼容,甚至出现错误。
53
53
<h4 id =" 3 " >代码布局</h4 >
54
54
<h5 id =" 3.1 " >缩进</h5 >
55
- 一个缩进级别四个空格。
56
- 连续行使用两种方式使封装元素成为一行:括号内隐式连接 和 悬挂式缩进
57
- 1、使用大中小括号内的隐式连接
55
+ 一个缩进级别四个空格。
56
+ * 连续行使用两种方式使封装元素成为一行:括号内垂直隐式连接和悬挂式缩进,使用悬挂式缩进应该注意第一行不应该有参数,连续行要使用进一步的缩进来区分。
58
57
``` Python
59
- # 垂直对齐 ,用开放定界符使成一行
58
+ # 垂直隐式连接对齐 ,用开放定界符使成一行
60
59
foo = func_name(var_one, var_two,
61
60
var_three, var_four)
62
61
63
- # 函数定义时需要再加一级缩进区分非参数代码行
62
+ # 悬挂式缩进, 函数定义时需要再加一级缩进区分非参数代码行
64
63
def long_function_name (
65
64
var_one , var_two , var_three ,
66
65
var_four ):
@@ -71,3 +70,47 @@ foo = long_function_name(
71
70
var_one, var_two,
72
71
var_three, var_four)
73
72
```
73
+ * 当 if 语句过长需要换行时,以下处理方法可以采用。
74
+ ``` Python
75
+ # 没有额外的缩进
76
+ if (this_is_one_thing and
77
+ that_is_another_thing):
78
+ do_something()
79
+
80
+ # 增加注释,用以区分函数体部分
81
+ # 支持语法高亮
82
+ if (this_is_one_thing and
83
+ that_is_another_thing):
84
+ # Since both conditions are true, we can frobnicate.
85
+ do_something()
86
+
87
+ # 连续行使用进一步的缩进和其他函数体区分
88
+ if (this_is_one_thing
89
+ and that_is_another_thing):
90
+ do_something()
91
+ ```
92
+ * 当闭环括号内元素跨行时,可以采用以下方式
93
+ ``` Python
94
+ my_list = [
95
+ 1 , 2 , 3 ,
96
+ 4 , 5 , 6 ,
97
+ ]
98
+ result = some_function_that_takes_arguments(
99
+ ' a' , ' b' , ' c' ,
100
+ ' d' , ' e' , ' f' ,
101
+ )
102
+ ```
103
+ <h5 id =" 3.2 " >Tabs or Spaces?</h5 >
104
+ 除非项目中已经约定了使用 tab 作为缩进,最好使用 space。
105
+ Python 3 不允许 tab 和 space 混用,同时混用了 tab 和 space 的 Python 2 代码应该被转换为仅使用 space。
106
+ <h5 id =" 3.3 " >代码行最大长度</h5 >
107
+ 将所有行限制为最多79个字符。
108
+ 对于具有较少结构限制(文档字符串或注释)的长文本块,行长度应限制为72个字符。
109
+ 当然了,不要问为啥非得是 79,72。背后也许有一个很是凄惨的故事。
110
+ 反斜杠有时可能仍然要用。 例如,又多又长的 with - 语句不能使用隐式连接,这时反斜杠是可以接受的:
111
+ ``` Python
112
+ with open (' /path/to/some/file/you/want/to/read' ) as file_1, \
113
+ open (' /path/to/some/file/being/written' , ' w' ) as file_2:
114
+ file_2.write(file_1.read())
115
+ ```
116
+ assert 语句也是如此。
0 commit comments