Python什么代码约定将参数拆分成行?

Are*_*ski 1 python coding-style python-2.7

将参数分成行的惯例是什么?是否有PEP对此进行制裁?我没有在PEP8中找到它.

file_like = f(self,
              path,
              mode=mode,
              buffering=buffering,
              encoding=encoding,
              errors=errors,
              newline=newline,
              line_buffering=line_buffering,
              **kwargs)
Run Code Online (Sandbox Code Playgroud)

ane*_*oid 6

当(B)最大线长度超过时,使用(A)垂直对准,有时即使不超过 - 以获得更好的可读性.

这些是该页面上的前3个示例,说明了它:

是:

# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# More indentation included to distinguish this from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

# Hanging indents should add a level.
foo = long_function_name(
    var_one, var_two,
    var_three, var_four)
Run Code Online (Sandbox Code Playgroud)

你问过的那个上面第一个例子变体,为了清晰起见,每行只有一个arg.

没有:

# Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
    var_three, var_four)
Run Code Online (Sandbox Code Playgroud)

并且(C),完成了名称给定的参数(关键字参数),这样您就不需要查询被调用函数中的参数.通常,每个非显而易见的参数都可以作为关键字参数传递,这些参数可以作为被调用函数的核心功能的标志或修饰符.例如:

> read_file('abc.txt', 1024, True)  # yes, you know 'abc.txt' is the filename
> # What are the 1024 and True for?
> # versus...
> read_file('abc.txt', max_lines=1024, output_as_list=True)  # now you know what it does.
Run Code Online (Sandbox Code Playgroud)

PS:@falsetru的回答并没有错.最大行长度是重新格式化代码的第一个原因.至于以这种方式对齐的具体原因,那就是垂直对齐.