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

2024-05-14 04:30:13 发布

您现在位置:Python中文网/ 问答频道 /正文

这种把论点分成几行的惯例是什么?有政治公众人物制裁这件事吗?我没有在PEP8找到它。在

file_like = f(self,
              path,
              mode=mode,
              buffering=buffering,
              encoding=encoding,
              errors=errors,
              newline=newline,
              line_buffering=line_buffering,
              **kwargs)

Tags: modelinenewline政治encodinglikefile公众
2条回答

^{}:如果不拆分为多行,则行长度将超过79个字符。在

当(B)将超过最大行长度时(A)Vertical Alignment将被使用,有时即使不会-为了更好的可读性。在

以下是该页的前3个示例:

Yes:

# 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)

您询问的是上面第一个示例的变体,为了清晰起见,每行只有一个arg。在

No:

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

和(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.

注:The answer by @falsetru没有错。最大行长度是重新格式化代码的第一个原因。至于为什么要这样对齐,那就是垂直对齐。在

相关问题 更多 >