Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indentation of closing parenthesis

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)
like image 664
binaryfunt Avatar asked Jun 01 '14 23:06

binaryfunt


People also ask

What is hanging indent in Python?

What is a Hanging Indent? A hanging indent is a type of indentation where the first line of a paragraph is not indented, but subsequent lines in the same paragraph are indented. In Google Docs, you can create a hanging indent by following these simple steps: Step 1: Select the text you want to format.

How do you close a bracket in Python?

According to the PEP 8 standard, there are two ways to line up the closing braces, brackets, or parentheses. First, line it up with the first non-whitespace character of the previous line. Second, line it up with the first character that starts the multi-line construct. What is this?


2 Answers

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.

like image 130
AXO Avatar answered Sep 17 '22 10:09

AXO


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.

like image 44
alecxe Avatar answered Sep 17 '22 10:09

alecxe