Formatting Parameter Blocks in Python
There are two ways recommended in PEP 8 to format the blocks with long parameter lists in Python:
# Arguments start on the next line
foo = long_function_name(
var_one, var_two,
var_three, var_four)
Another way is this:
# Arguments start on the same line
foo = long_function_name(var_one, var_two,
var_three, var_four)
I always prefer the first option, as the other one is problematic for a few reasons.
For example, if you have two blocks with such indentation, the indent will be jumping:
# Two functions formatted in such a way don't look good, the indent is jumping
foo = long_function_name(var_one, var_two,
var_three, var_four)
bar = another_func(var_one, var_two,
var_three, var_four)
Another reason is that the indentation will be broken if you, for example, rename the function or make it a class method:
foo = long_function_name(var_one, var_two,
var_three, var_four)
# After renaming the function (or the `foo` variable),
# we need to fix the indentation on the following lines.
foo = renamed_long_function_name(var_one, var_two,
var_three, var_four)
foo = self.long_function_name(var_one, var_two,
var_three, var_four)
Now, besides the rename / refactoring, we also need to fix the indentation in the following lines.
The first way of formatting, on the other hand, works well in these cases:
# Two functions near each other, indentation is not jumping
foo = long_function_name(
var_one, var_two,
var_three, var_four)
bar = another_func(
var_one, var_two,
var_three, var_four)
# Rename / move to class, arguments formatting doesn't need to be changed
foo = long_function_name(
var_one, var_two,
var_three, var_four)
foo = renamed_long_function_name(
var_one, var_two,
var_three, var_four)
foo = self.long_function_name(
var_one, var_two,
var_three, var_four)
Along with more convenience during development, this also results in better diffs, so it will make the code review process easier for a reviewer.