我想知道这是错误的还是被认为是不好的做法或其他什么:
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/