bin*_*unt 7 python coding-style pep8
注意:不要相信原始问题中的任何内容是正确的,请转到底部进行更新。
原始问题
我相信 PEP8 风格指南说
some_kind_of_list = [
1, 2, 3,
4, 5, 6
]
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2
):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
和
some_kind_of_list = [
1, 2, 3,
4, 5, 6
]
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2
):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
是可以接受的,但是使用其中一个是否有意义,例如,如果我在以后的生活中转向 C++?
更新
为了直接记录,函数定义的常见样式是:
some_kind_of_list = [
1, 2, 3,
4, 5, 6
]
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2
):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
而对于函数调用,它是:
some_kind_of_list = [
1, 2, 3,
4, 5, 6
]
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2
):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
我通常会用
some_kind_of_list = [
1, 2, 3,
4, 5, 6,
]
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2,
):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
这样缩进将是可区分的。在最后一个参数的末尾还有一个逗号可以在不更改其他行的情况下稍后添加新的 args,这对于 git-blaming 目的来说通常是一件好事,并且可以减少 diffs 中的混乱。
我并不特别关心这种风格,我刚刚检查过,它不在 PEP 8 中,并且它可能会干扰任何给定 IDE 折叠代码块的能力(它与我在 Python 工作中使用的那种风格有关):
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2
):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
我宁愿你避免它并执行以下操作:
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
或者
def function_that_takes_long_arguments(long_argument_1,
long_argument_2):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
不过,无论哪种方式,列表样式都可能很好。