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)
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.
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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With