9

PEP 8 regarding hanging indent:

When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.

Is there any explicit documentation about about "sub-arguments"? For example:

some_method(argument_one, argument_two, argument_three=[
    'parameter_one',
    'parameter_two',
    'parameter_three',
    ])

As opposed to:

some_method(
    argument_one,
    argument_two,
    argument_three=[
        'parameter_one',
        'parameter_two',
        'parameter_three',
        ]
    )

Preferably links to official discussions only.

1
  • The best thing to do is strive for readability. Usually that means to follow guidelines, but sometimes not. Apr 3, 2014 at 23:03

2 Answers 2

11

From PEP 8's "Other Recommendations" section:

Compound statements (multiple statements on the same line) are generally discouraged.

With this recommendation in mind, your 2nd example is probably more in line with the PEP 8 style guide, as it avoids compounding the method invocation and list construction on the same line. The 2nd example reads a little easier too.

7

I realize this question is years old, but I wanted to share my thoughts and the approach that has worked for me, especially because I am legally blind. (I specifically aim for shallow nesting and to keep my function calls as simple and readable as possible.)

In order to avoid potentially tricky readability situations like this, I will actually pre-define the list (or any item that creates a complex, messy function call) as a variable, clearly named with some form of indication that it is temporary:

# Build temp argument to avoid messy function call.
arg3_tmp = [
    'parameter1',
    'parameter2',
    'parameter3',
]
some_method(argument1, argument2, arg3_tmp)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.