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

/*

*/

当前回答

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

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

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

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

按下Shift +我。

按#(或Shift+3)。

按Esc。

其他回答

多行注释实际上在Python中并不存在。下面的示例包含一个未赋值的字符串,Python将验证该字符串是否存在语法错误。

一些文本编辑器,如notepad++,为我们提供了注释掉一段书面代码或单词的快捷方式。

def foo():
    "This is a doc string."
    # A single line comment
    """
       This
       is a multiline
       comment/String
    """
    """
    print "This is a sample foo function"
    print "This function has no arguments"
    """
    return True

此外,Ctrl + K是notepad++中阻止注释的快捷键。它在选定的每一行前面添加一个#。Ctrl + Shift + K表示取消注释。

如果在带有代码的一行中编写注释,则必须在#符号之前留下2个空格,在#符号之前留下1个空格

print("Hello World")  # printing

如果在新行上写注释,必须写注释,在#符号中留下1个空格kn

# single line comment

要编写超过一行的注释,需要使用3个引号

"""
This is a comment
written in
more than just one line
"""

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

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

是的,两者都可以使用:

'''
Comments
'''

and

"""
Comments
"""

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

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