在 Python 3.5 中,IDE 如何将三引号 (""") 视为注释?

Noa*_*oah 10 python comments

我的CS老师告诉我,“””三引号用作注释,但我把它当作带有换行符和缩进的字符串来学习。这让我思考——Python是否完全在相关语句之外使用三引号行?

"""is this completely ignored like a comment"""
Run Code Online (Sandbox Code Playgroud)

-或者,计算机实际上正在考虑这一点吗?

The*_*Guy 11

三重引号字符串被许多开发人员用作注释,但它实际上不是注释,它类似于 python 中的常规字符串,但它允许字符串为多行。您会发现没有关于三重引号字符串的官方参考作为注释。

在Python中,只有一种以hash开头的注释#,并且只能包含一行文本。

然而,根据PEP 257,它可以用作文档字符串,这又不是真正的注释。

def foo():
    """
    Developer friendly text for describing the purpose of function
    Some test cases used by different unit testing libraries
    """
   <body of the function>
   
Run Code Online (Sandbox Code Playgroud)

您可以将它们分配给变量,就像处理单引号字符串一样:

x = """a multi-line text
enclosed by
triple quotes
"""
Run Code Online (Sandbox Code Playgroud)

此外,如果你尝试在 repl 中,三重引号字符串会被打印出来,如果它真的是一条注释,它应该被打印吗?:

>>> #comment
>>> """triple quoted"""
'triple quoted'
Run Code Online (Sandbox Code Playgroud)