Python 常用常用 PEP8 编码规范详解编码规范详解
主要介绍了Python 常用 PEP8 编码规范详解的相关资料,需要的朋友可以参考下
Python 常用常用 PEP8 编码规范编码规范
代码布局
缩进
每级缩进用4个空格。
括号中使用垂直隐式缩进或使用悬挂缩进。
EXAMPLE:
# (垂直隐式缩进)对准左括号
foo = long_function_name(var_one, var_two,
var_three, var_four)
# (悬挂缩进) 一般情况只需多一层缩进
foo = long_function_name(
var_one, var_two,
var_three, var_four)
# (悬挂缩进) 但下面情况, 需再加多一层缩进, 和后续的语句块区分开来
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
# 右括号回退
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)
错误示范错误示范:
# 不使用垂直对齐时,第一行不能有参数。
foo = long_function_name(var_one, var_two,
var_three, var_four)
# 参数的悬挂缩进和后续代码块缩进不能区别。
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
# 右括号不回退,不推荐
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)
最大行宽
1. 每行最大行宽不超过 79 个字符
2. 一般续行可使用反斜杠
3. 括号内续行不需要使用反斜杠
EXAMPLE:
# 无括号续行, 利用反斜杠
with open('/path/to/some/file/you/want/to/read') as file_1, \
评论0
最新资源