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

/*

*/

当前回答

在其他答案中,我发现最简单的方法是使用IDE注释函数,该函数使用Python注释支持#。

我正在使用Anaconda Spyder,它有:

Ctrl + 1 -注释/取消注释 Ctrl + 4 -注释代码块 Ctrl + 5 -取消注释代码块

它可以用#注释/取消注释一行/多行代码。

我觉得这是最简单的。

例如,一个块注释:

# =============================================================================
#     Sample Commented code in spyder
#  Hello, World!
# =============================================================================

其他回答

在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
   """

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

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

不幸的是,字符串化不能总是用于注释!因此,坚持在每行前加上#的标准更安全。

这里有一个例子:

test1 = [1, 2, 3, 4,]       # test1 contains 4 integers

test2 = [1, 2, '''3, 4,'''] # test2 contains 2 integers **and the string** '3, 4,'

要注释掉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__

是的,两者都可以使用:

'''
Comments
'''

and

"""
Comments
"""

但是,在IDE中运行时,你需要记住的唯一一件事是,你必须“运行”整个文件,以接受多行代码。逐行“RUN”将无法正常工作,并将显示错误。