Python中的缩进注释

问题描述 投票:2回答:1

我想知道这是错误的还是被认为是不好的做法或任何事情:

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,这是一个意外的缩进,但代码将执行。

这种不好的风格是否有更好的做法来做出这种评论?

python coding-style pep8
1个回答
4
投票

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

块注释通常适用于跟随它们的一些(或所有)代码,并缩进到与该代码相同的级别。块注释的每一行都以#和单个空格开头(除非它是注释中的缩进文本)。

(强调我的)

我相信评论的正确缩进将是 -

# 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()

对我而言,这看起来比在函数调用的同一行上有第一条注释要好,而其余的则在它下面。

© www.soinside.com 2019 - 2024. All rights reserved.