Closing braces with Python

There's no conflict here, since PEP8 specifically says:

The closing brace/bracket/parenthesis on multiline constructs may either line up under the first non-whitespace character of the last line of list, as in:

my_list = [
    1, 2, 3,
    4, 5, 6,
    ]
result = some_function_that_takes_arguments(
    'a', 'b', 'c',
    'd', 'e', 'f',
    )

or it may be lined up under the first character of the line that starts the multiline construct, as in:

my_list = [
    1, 2, 3,
    4, 5, 6,
]
result = some_function_that_takes_arguments(
    'a', 'b', 'c',
    'd', 'e', 'f',
)

so both conventions are acceptable.

I personally prefer the latter convention, but that's just me.


The two sections you mention are different in that the first is about continuation lines that are followed by a block (such as a multiline def or if statement) while the second is about closing braces and parentheses on affectation and function calls. When starting a block, you wouldn't want to put the closing parenthesis at the beginning of the next line because going back to the original indentation conveys the end of the block. A few examples that clearly look odd:

def long_function_foo(
    var_one, var_two, var_three,
    var_four
):
    print('This code really looks out of place')

def long_function_bar(
   var_one,
   var_two
):
    print('and so does this one')

PEP8 allows what they call vertical alignment, and many examples in various PEPs use this convention which has become an automated feature of Python IDEs:

def long_function_name(var_one, var_two, var_three,
                       var_four, var_five):
    """Documentation would go here, which makes it look better."""
    print(var_one + var_two + var_three)

But I personally avoid it. This is an opinion-based topic, but I don't like relying on alignment through a specific number of spaces. It's tedious to maintain and relies too much on IDE smart indents. I prefer this notation, which is allowed by PEP8 but doesn't seem as popular. Note the double-indent used for distinction from the function body:

def long_function_name(
        alpha, bravo, charlie, delta, echo, foxtrot,
        hotel, indiana):
    """Documentation would go here."""
    print(var_one + var_two + var_three)

When it comes to function calls and assignments, PEP8 doesn't have a clear answer. One might indent the closing parenthesis, as a way to mimic how blocks end when the next instruction is less indented.

foo = bar(
    1, 2, 3
    )

Vertical alignment is very popular and I'll admit it looks good, but again I don't want to force indentation size on future readers of my code so I avoid this:

foo = bar(1, 2, 3, 5, 6, 7, 8, 9,
          10, 11, 12, 13, 14)

Or one can also put the closing brace/parenthesis aligned to the left:

foo = bar(
    1, 2, 3
)

Coming from a C++, Java and JavaScript background, I use the latter option. Technically, you could also put the closing parenthesis on the same line as the arguments, but then it makes it look like an indented code block too much for my tastes, and it's not something I've really seen people do.