if 语句是否应该被空行包围以遵循 PEP8?

Should if statements be surrounded by blank lines to follow PEP8?

我读过 the blank line section in pep8。它说我们应该用空行包围顶级函数,但它没有说明 if 语句。

我已经深入研究了 Python 中的顶级是什么,它似乎是任何没有缩进的东西。这是否意味着 if 语句应该被空行包围? if 语句是否被视为函数?

我在堆栈溢出上找不到任何关于此的内容。

"""
my cool and interesting program!
"""

import this_module
import that_module
import another_module


def procedure_to_do_this(*args):
    if args:
        print("the caller says %s!" % args[0])
    elif not args:
        print("the caller is too shy to say anything ;c")
    else:
        print("if you've reached this point, there's no turning back")

    return something


procedure_to_do_this()

是您通常形成 if/elif/else 结构的方式,但是在大多数情况下,该结构是否符合代码的其余部分对您来说是主观的,当然您可以更改它,如果你觉得其中一个条件子句中的代码太多并且你想区分条件,你可以在一个条件结束后添加一个额外的换行符,例如:

if this:
    <a lot of code>
    <a lot of nice code>
    <a lot of long code>
<newline>
elif that:
    <a lot of other code>

等等。

永远记住,PEP8 只是一个样式指南[行],它绝不是规定代码的整体设计,因为它本身是主观的,人们认为好的样式看起来是什么那里有好的或特定的代码。

并非所有代码在某种风格下看起来都很好,所以这就是为什么它是一个指南,而不是 Python 严格执行并已实施到解释器中的东西。