自动保存在 VS 代码中 Python 中的注释和函数头之间添加两个空行

Auto save adds two empty lines between comment and function header in Python in VS code

我在Python中用VS code写代码。如果我在函数前添加注释并点击保存按钮,VS 代码会添加两个空行:

# comment


def MyMethod():
    return 0

在设置中我看到我使用了 autopep8 格式化程序:

我找不到导致这个恼人问题的原因。也许我可以在某处配置设置?

这是由于 Python 的代码约定 (PEP8)。 Pylance 将在保存时更正您的代码。
这是 PEP8 的摘录,您可以在其中找到 here:
You should use two spaces after a sentence-ending period.

如果评论描述了您的函数,请尝试使用 Docstrings。

记录函数行为的注释应放在函数内部,就在签名下方。 如果它没有描述功能(放在功能之外),它应该有那些空行。 不要乱用语言的约定,这是非常糟糕的主意。

编辑,进一步说明:

"""Module-level comment
"""


def function(): 
    """Function level comment. 
    There are multiple conventions explaining how
    a comment's body should be formed.
    """
    return 0