gpt4 book ai didi

python - PEP 8 : E128 sometimes requires spaces and sometimes does not

转载 作者:行者123 更新时间:2023-12-05 04:33:46 25 4
gpt4 key购买 nike

post's answer建议 PEP 8: E128 要求在第一行之后的行上有空格,当它们都包含在括号内时。但是,对于 if 语句,情况似乎并非如此。您可以看到前三行没有警告,而后三行有警告,因为它们确实有一个空格:

我错过了什么吗?如果有帮助,我正在使用 Pycharm 社区版。

使用的代码:

def main(user_input):
if (";" not in user_input
and "DROP " not in user_input
and "SELECT " not in user_input
and "FROM " not in user_input
and "DELETE " not in user_input
and '"' not in user_input
and ";" not in user_input
and "=" not in user_input
and ">" not in user_input
and "<" not in user_input):
pass
else:
exit()

编辑:因为有点困惑,这应该是正确的缩进:https://i.imgur.com/FqsJjkc.png这是 Pycharm 认为正确的一个 https://i.imgur.com/6DmejGi.png .除非我弄错了(可能是这样!)。

最佳答案

PEP-8 注意到 if ( 提供了一个自然的 4 空格缩进,它允许继续在括号之后开始:

if (";" not in user_input
and "DROP " not in user_input
...

你的检查器显然不关心你是否写

if (";" not in user_input
and "DROP " not in user_input
...

相反,但是,如果您确实使用该缩进,则必须继续使用相同的缩进。您的 FROM 行是偏离该缩进的第一行。


这很常见,PEP 甚至提供了一个链接到 this paragraph 的 anchor :

When the conditional part of an if-statement is long enough to requirethat it be written across multiple lines, it's worth noting that thecombination of a two character keyword (i.e. if), plus a single space,plus an opening parenthesis creates a natural 4-space indent for thesubsequent lines of the multiline conditional. This can produce avisual conflict with the indented suite of code nested inside theif-statement, which would also naturally be indented to 4 spaces. ThisPEP takes no explicit position on how (or whether) to further visuallydistinguish such conditional lines from the nested suite inside theif-statement. Acceptable options in this situation include, but arenot limited to:

# No extra indentation.
if (this_is_one_thing and
that_is_another_thing):
do_something()

# Add a comment, which will provide some distinction in editors
# supporting syntax highlighting.
if (this_is_one_thing and
that_is_another_thing):
# Since both conditions are true, we can frobnicate.
do_something()

# Add some extra indentation on the conditional continuation line.
if (this_is_one_thing
and that_is_another_thing):
do_something()

(Also see the discussion of whether to break before or after binaryoperators below.)

关于python - PEP 8 : E128 sometimes requires spaces and sometimes does not,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71338381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com