Python 中的缩进评论

Indented comments in Python

我想知道这是错误的还是被认为是不好的做法或其他什么:

def my_fuction():
    """This function does something."""
    pass

my_function()  # Yes I could write a comment here
    # But sometimes I need more space and make it clear
    # that a comment belongs to a certain part of my code

如您所见,我在函数调用下方缩进了评论,以便为该特定调用留下具体说明/评论。我将其缩进以明确此注释属于此代码调用。 PyCharm 警告我这是根据 PEP8 的意外缩进,但代码会执行。

这种风格不好吗?有没有更好的做法来发表这种评论?

我相信这属于 Block Comments part of PEP0008 ,建议 -

Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).

(强调我的)

我认为评论的正确缩进应该是 -

# Yes I could write a comment here
# But sometimes I need more space and make it clear
# that a comment belongs to a certain part of my code
my_function()

在我看来,这比将第一个注释与函数调用放在同一行,而其余的在它下面更好。