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

/*

*/

当前回答

如果你写了评论

"""
long comment here
"""

在脚本中间,Python/linters不会识别它。折叠将是混乱的,因为上面的注释不是标准建议的一部分。用起来更好

# Long comment
# here.

如果你使用Vim,你可以添加像评论这样的插件。通过按Vjgcc自动注释掉长行注释。其中Vj选择了两行代码,gcc将它们注释掉。

如果你不想使用像上面这样的插件,你可以使用搜索和替换

:.,.+1s/^/# /g

这将用#替换当前和下一行的第一个字符。

其他回答

Visual Studio Code通用官方多行注释切换。类似于Xcode的快捷方式。

macOS:选择code-block,然后选择⌘+/

Windows:选择code-block,然后按Ctrl+/

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

这里有一个例子:

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

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

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中的内嵌注释以哈希字符开始。

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.
'''

没有多行注释这样的特性。#是注释一行代码的唯一方法。 你们中的许多人回答“一个评论”,这是他们的解决方案。

这看起来是可行的,但在Python内部,“'接受作为常规字符串封装的行,解释器不会像使用#的注释那样忽略它。

点击这里查看官方文件