python - Python 中的缩进注释

标签 python coding-style pep8

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

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

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

关于python - Python 中的缩进注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32047005/

相关文章:

python - Softlayer API : change root password and ssh-key operation

c++ - 使用函数调用仿函数

python - flake8 和 "old style class declaration"

python - Django 管理中的 MultiValueDictKeyError

python - 什么是从 python dict 或 list 中深度复制数据的快速 pythonic 方法?

python - 如何将新的默认软件包添加到virtualenv?

javascript - 将高度和宽度值设置为负值?

C 错误处理编码风格

python - 从函数调用常量时,(PEP8)换行的正确方法是什么?

python - 用 Python 关闭大括号