8

Note: Don't believe anything in the original question is correct, go to the bottom for an update.


Original question

I believe the PEP8 style guide says that both

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

def function_that_takes_long_arguments(
    long_argument_1,
    long_argument_2
    ):
    return long_argument_1

and

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

def function_that_takes_long_arguments(
    long_argument_1,
    long_argument_2
):
    return long_argument_1

are acceptable, but does it make sense to use one or the other, e.g., if I move onto C++ later on in my life?


Update

To set the record straight, the common style for function definitions is:

def function_that_takes_long_arguments(
        long_argument_1,
        long_argument_2):
    pass  # Note the extra indentation in the 2 lines above

# or

def function_that_takes_long_arguments(long_argument_1,
                                       long_argument_2):
    pass

whereas for function calls it's:

function_that_takes_long_arguments(
    long_argument_1,
    long_argument_2
)

# or

function_that_takes_long_arguments(
    long_argument_1,
    long_argument_2)

# or

function_that_takes_long_arguments(long_argument_1,
                                   long_argument_2)
binaryfunt
  • 6,401
  • 5
  • 37
  • 59
  • 1
    Try not to lose sleep over this. Do whatever is more legible for you. – Roy Iacob Jun 02 '14 at 00:04
  • In `https://www.python.org/dev/peps/pep-0008/#code-lay-out`, I didn't find that trailing/closing parenthese in a separate line for function call. – Kai Jul 30 '20 at 10:20

3 Answers3

6

I'd usually use

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

def function_that_takes_long_arguments(
    long_argument_1,
    long_argument_2,
):
    return long_argument_1

This way indentation will be distinguishable. Also having a comma at the end of the last argument makes it possible to add new args later without changing other lines which is usually a good thing for git-blaming purposes and makes less clutter in diffs.

Community
  • 1
  • 1
AXO
  • 8,198
  • 6
  • 62
  • 63
2

pep8 python style guide checker doesn't think both snippets are acceptable.

First option:

$ pep8 test_pep.py 
test_pep.py:10:5: E125 continuation line with same indent as next logical line

Second option (no warnings):

$ pep8 test_pep.py 
$

As you see, for the list it is okay to use both. But for the function, the second approach is preferred since in the first snippet the function body is indented as the previous line and it makes a negative impact on the readability.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

I don't particularly care for this style, and I just checked, it's not in PEP 8, and it may interfere with any given IDE's ability to collapse code blocks (it does with the one I use at work for Python):

def function_that_takes_long_arguments(
    long_argument_1,
    long_argument_2
):
    return long_argument_1

I'd rather you avoid it and do the following instead:

def function_that_takes_long_arguments(
  long_argument_1,
  long_argument_2):
    return long_argument_1

or

def function_that_takes_long_arguments(long_argument_1,
                                       long_argument_2):
    return long_argument_1

The list style is probably fine either way, though.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331