1

When calling function with long argument lists, should the closing parenthesis be on a separate line? For example:

import module1.module2

def main():

    # alternative 1, closing parenthesis on separate line
    x=3
    y=4
    my_result_name_1, my_result_name_2 = module1.module2.function3(
        argument_name1, argument_name2, keyword_argument=(x,y)
    )
    print(my_result_name_1)

    # alternative 2, closing parenthesis on same line as last argument
    x=3
    y=4
    my_result_name_1, my_result_name_2 = module1.module2.function3(
        argument_name1, argument_name2, keyword_argument=(x,y))
    print(my_result_name_1)

    return

if __name__ == '__main__':
    main()

The examples in PEP8 are a little bit confusing to me. First they have this example:

# Hanging indents should add a level.
foo = long_function_name(
    var_one, var_two,
    var_three, var_four)

Then later, they have this:

my_list = [
    1, 2, 3,
    4, 5, 6,
]

Which style is preferred?

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • Relevant: http://stackoverflow.com/questions/23985688/indentation-of-closing-parenthesis – Chris_Rands Oct 24 '16 at 09:02
  • I'm pretty sure both styles are acceptable. I think closing parens on the same line look better on longer lines, whereas in a different line they are prettier on short ones. – alexpeits Oct 24 '16 at 09:02
  • 1
    I'd go for: 1) Always one argument per line, independently if it's long or not. 2) Closing parens in its own line. Why? Because in this way every time you change a single argument or add/remove a single argument this changes only that line and it's very easy to understand the `diff`s in VSCs. – Bakuriu Oct 24 '16 at 09:13
  • @Bakuriu Thanks for the suggestion. I think I go with this! It makes the code more readable than the other alternatives, and prefer readability to conciseness. – Håkon Hægland Oct 24 '16 at 09:48

1 Answers1

1

Personally, when I've only a need for a second additional line; I put the open and close brackets in-line, eg:

foo = long_function_name(var_one, var_two,
                         var_three, var_for)

But if I'm going to have more that one additional line, I keep the content on separate lines, eg:

my_list = [
    1, 2, 3,
    4, 5, 6,
    7, 8, 9
]

Really it is up to you to decide how you want to do it. For me, the most important things are readability and consistency.

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
Karim Tabet
  • 1,789
  • 1
  • 14
  • 35
  • Thanks for the suggestion! Looks good, just one thing: According to PEP8: *"Arguments on first line are forbidden when not using vertical alignment"*. So your first example is not recommended according to PEP8? – Håkon Hægland Oct 24 '16 at 09:23
  • Hmm, it's a bit vague. What has to be vertically aligned exactly? How would you align the in-line arguments vertically? – Karim Tabet Oct 24 '16 at 09:44
  • 1
    I edited your question to show how I would interpret the statement, just roll the edit back if you don't agree :) – Håkon Hægland Oct 24 '16 at 09:53
  • I think I agree :) Definitely more readable than how I had it the first go round. – Karim Tabet Oct 24 '16 at 10:07