如何做多行注释?大多数语言都有块注释符号,比如:

/*

*/

当前回答

选择要注释的行,然后使用Ctrl + ?在Sublime文本编辑器中注释或取消注释Python代码。

对于单行,可以使用Shift + #。

其他回答

Python 2.7.13:

单:

"A sample single line comment "

多行:

"""
A sample
multiline comment
on PyCharm
"""

这可以在Vim文本编辑器中完成。

转到评论区第一行的开头。

按“Ctrl+V”进入可视模式。

使用方向键选择要注释的所有行。

按下Shift +我。

按#(或Shift+3)。

按Esc。

我认为它没有,除了一个多行字符串没有被处理。然而,大多数(如果不是所有的话)Python ide都有一个短键来“注释掉”多行代码。

我建议不要在多行注释中使用“”!

这里有一个简单的例子来强调可能被认为是意外的行为:

print('{}\n{}'.format(
    'I am a string',
    """
    Some people consider me a
    multi-line comment, but
    """
    'clearly I am also a string'
    )
)

现在看一下输出:

I am a string

    Some people consider me a
    multi-line comment, but
    clearly I am also a string

多行字符串不被视为注释,但它与'显然我也是一个字符串'连接起来,形成一个单一的字符串。

如果您想注释多行,请根据PEP 8指南执行:

print('{}\n{}'.format(
    'I am a string',
    # Some people consider me a
    # multi-line comment, but
    'clearly I am also a string'
    )
)

输出:

I am a string
clearly I am also a string

要注释掉Python中的多行代码,只需在每一行上使用# single-line注释:

# This is comment 1
# This is comment 2 
# This is comment 3

要在Python中编写“正确的”多行注释,就是使用带有“”语法的多行字符串 Python有文档字符串(或文档字符串)特性。它为程序员提供了一种简单的方法,可以为每个Python模块、函数、类和方法添加快速注释。

'''
This is
multiline
comment
'''

另外,提到您可以通过这样的类对象访问docstring

myobj.__doc__