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

/*

*/

当前回答

在Python 2.7中,多行注释是:

"""
This is a
multilline comment
"""

如果你在一个类中,你应该正确地制表。

例如:

class weather2():
   """
   def getStatus_code(self, url):
       world.url = url
       result = requests.get(url)
       return result.status_code
   """

其他回答

你可以使用三引号字符串。当它们不是docstring(类/函数/模块中的第一件事)时,它们将被忽略。

'''
This is a multiline
comment.
'''

(确保适当缩进前导" '以避免出现IndentationError。)

Guido van Rossum (Python的创造者)在推特上发布了这条“专业提示”。

然而,Python的风格指南PEP8倾向于使用连续的单行注释,像这样:

# This is a multiline
# comment.

...这也是你在很多项目中会发现的。文本编辑器通常有一个快捷方式来轻松做到这一点。

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

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

AFAIK, Python没有块注释。对于注释单个行,可以使用#字符。

如果你正在使用notepad++,有一个块注释的快捷方式。我相信其他的像gVim和Emacs也有类似的特性。

Python中的多行注释:

对我来说,“’”和“”都管用。

例子:

a = 10
b = 20
c = a+b
'''
print ('hello')
'''
print ('Addition is: ', a+b)

例子:

a = 10
b = 20
c = a+b
"""
print('hello')
"""
print('Addition is: ', a+b)

Python 2.7.13:

单:

"A sample single line comment "

多行:

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