简体   繁体   中英

Python proper indentation for variable assigned to value of multi-line function?

What is the proper indentation for a variable that is set to the output of a multiple line function?

I've seen it pushed way over to the equals sign like this:

dept_alias_valid = RegexValidator(
                  '^(?!.*--)(?!.*__)(?!.*-_)(?!.*_-)([@]+[a-z][a-z\-_]+[a-z]+)$', 
                  "Alias must: start with @ and the remainder can contain only lowercase letters a-z _underscores -dashes with neither trailing nor back-to-back special characters and spaces are right out!"
                   )

And I've also seen it like this:

dept_alias_valid = RegexValidator(
  '^(?!.*--)(?!.*__)(?!.*-_)(?!.*_-)([@]+[a-z][a-z\-_]+[a-z]+)$', 
  "Alias must: start with @ and the remainder can contain only lowercase letters a-z _underscores -dashes with neither trailing nor back-to-back special characters and spaces are right out!"
)

Depends on what you mean by "proper".


As far as PEP 8 is concerned, both styles are valid…

Yes

# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# More indentation included to distinguish this from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

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

… but both of your examples are invalid for other reasons.


The second one is indented 2 spaces, while the first one is indented 19 spaces, but:

Use 4 spaces per indentation level.

(It isn't entirely clear that "more indentation" should be a fixed number of indent levels, but I'm pretty sure that's the intention.)

However, it also says that:

The 4-space rule is optional for continuation lines.

Of course all of PEP 8 is optional, and especially so for code that isn't meant for the stdlib, but this is apparently especially optional.


And both of them go way beyond the right edge of the window:

Limit all lines to a maximum of 79 characters.

For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters.


And the first one puts the closing paren in the wrong place:

The closing brace/bracket/parenthesis on multiline constructs may either line up under the first non-whitespace character of the last line of list, as in:

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

… or it may be lined up under the first character of the line that starts the multiline construct, as in:

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

Your second example does the second version right; your first example does the first version wrong.


However, there is an obvious advantage to the second style (properly fixed to use a 4-space indent) here: The whole reason you're splitting this onto multiple lines is to make it easier to fit the text into the window (even if you haven't succeeded). The first style wastes an extra 14 characters on each line, so it has less effect.

It's also worth noting that black and other automated code formatters will change your first example into your second (again, except with a 4-space indent).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM