简体   繁体   English

Python为分配给多行函数值的变量正确缩进?

[英]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… PEP 8而言,两种样式均有效……

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: 第二个缩进2个空格,而第一个缩进19个空格,但是:

Use 4 spaces per indentation level. 每个缩进级别使用4个空格。

(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. 对于连续行,4空格规则是可选的。

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. 当然,所有PEP 8都是可选的,尤其是对于不是stdlib的代码而言,这尤其是可选的。


And both of them go way beyond the right edge of the window: 它们都超出了窗口的右边缘:

Limit all lines to a maximum of 79 characters. 限制所有行最多79个字符。

For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters. 为了使较长的文本块具有较少的结构限制(文档字符串或注释),行长应限制为72个字符。


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). 但是,这里的第二种样式有一个明显的优势(正确固定为使用4个空格的缩进):将其拆分为多行的全部原因是为了使文本更适合窗口(即使您还没有成功)。 The first style wastes an extra 14 characters on each line, so it has less effect. 第一种样式在每行上浪费了额外的14个字符,因此效果较小。

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). 还要注意的是, black和其他自动代码格式化程序会将第一个示例更改为第二个示例(同样,除了4空格缩进以外)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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