What is this convention of splitting arguments into lines? Is there a PEP that sanctions this? I did not find it in PEP8.
file_like = f(self,
path,
mode=mode,
buffering=buffering,
encoding=encoding,
errors=errors,
newline=newline,
line_buffering=line_buffering,
**kwargs)
答案 0 :(得分:6)
It's for the (A) Vertical Alignment to be used when (B) Max Line Length will get exceeded, and sometimes even if it won't - for better readability.
These are the first 3 examples on that page illustrating it:
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)
The one you asked about is a variation of the first example above, with only one arg per line for clarity.
No:
# Arguments on first line forbidden when not using vertical alignment. foo = long_function_name(var_one, var_two, var_three, var_four)
And (C), the arguments with the name given (keyword arguments) is done so that you don't need to lookup what the parameters are in the called function. Typically, every non-obvious parameter which may act like a flag or modifier to the core functionality of the called function should be passed as a keyword argument. Ex:
> 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.
PS: The answer by @falsetru isn't wrong. Max line length is the first reason to re-format your code. As for the specific reason to align it that way, that's vertical alignment.
答案 1 :(得分:3)
Maximum Line Length
: If it was not splitted into multiple lines, the line length would be more than 79 characters.