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

/*

*/

当前回答

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

其他回答

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

Python 2.7.13:

单:

"A sample single line comment "

多行:

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

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

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

你可以使用下面的方法。这叫做DockString。

def my_function(arg1):
    """
    Summary line.
    Extended description of function.
    Parameters:
    arg1 (int): Description of arg1
    Returns:
    int: Description of return value
    """
    return arg1

print my_function.__doc__

Python中的内嵌注释以哈希字符开始。

hello = "Hello!" # This is an inline comment
print(hello)

你好!

注意,字符串文字中的哈希字符只是一个哈希字符。

dial = "Dial #100 to make an emergency call."
print(dial)

拨打100拨打紧急电话。

散列字符也可以用于单行或多行注释。

hello = "Hello"
world = "World"
# First print hello
# And print world
print(hello)
print(world)

你好 世界

用三双引号将文本括起来以支持docstring。

def say_hello(name):
    """
    This is docstring comment and
    it's support multi line.
    :param name it's your name
    :type name str
    """
    return "Hello " + name + '!'


print(say_hello("John"))

你好约翰!

在文本中加上三个单引号作为块注释。

'''
I don't care the parameters and
docstrings here.
'''