如何在Python中创建多行注释?

1406

如何编写多行注释?大多数语言都有块注释符号,例如:

/*

*/

4
我想,作为一种解释型语言,就像sh、bash或zsh一样,使用#作为注释的唯一方式是有道理的。我猜这样做可以让Python脚本更容易被解释。 - Victor Zamanian
1
我知道这个答案很旧,但我遇到了同样的问题才找到它。接受的答案确实有效,尽管我不太了解Python的细节,也不知道为什么它可能不正确(根据ADTC的说法)。 - Brandon Barney
7
让我向您解释这个问题。被接受的答案使用''', 实际上创建了一个没有作用的多行字符串。技术上讲,那不是注释。例如,你可以写k = '''假注释,真字符串'''。然后,打印(k)来看看ADTC是什么意思。 - pinyotae
3
现在我理解得多清楚了。我习惯使用 VBA,其中创建未使用的字符串会导致错误。我没有意识到 Python 只是忽略它。这仍然适用于调试和学习,但对于实际开发来说不是一个好的做法。 - Brandon Barney
在Python源代码中,如果您打破了一行长代码,编辑器会自动缩进它,以显示该断行实际上是前一行的一部分。如果我要打破一行伪代码,那么我应该这样做吗? - alpha_989
显示剩余3条评论
27个回答

7

我建议不要使用 """ 作为多行注释!

以下是一个简单的例子,以突显这可能被视为意外行为:

print('{}\n{}'.format(
    'I am a string',
    """
    Some people consider me a
    multi-line comment, but
    """
    'clearly I am also a string'
    )
)

现在来看一下输出结果:

I am a string

    Some people consider me a
    multi-line comment, but
    clearly I am also a string

多行字符串没有被视为注释,而是与'clearly I'm also a string'连接在一起形成单个字符串。 如果你想要注释多行,请遵循PEP 8的指南:
print('{}\n{}'.format(
    'I am a string',
    # Some people consider me a
    # multi-line comment, but
    'clearly I am also a string'
    )
)

输出:

I am a string
clearly I am also a string

5

好的,您可以尝试这样做(在引用时,第一个问题的输入应该用'引用):

"""
print("What's your name? ")
myName = input()
print("It's nice to meet you " + myName)
print("Number of characters is ")
print(len(myName))
age = input("What's your age? ")
print("You will be " + str(int(age)+1) + " next year.")

"""
a = input()
print(a)
print(a*5)

任何被"""包含的内容都将被注释掉。

如果你想要单行注释,那么就用 #


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)

4
如果您在代码行中写评论,必须在井号(#)前留下2个空格,并在井号(#)前留下1个空格。
print("Hello World")  # printing

如果您在新的一行上编写评论,必须在#符号后留下1个空格以编写评论。
# single line comment

如果你需要编写超过一行的注释,可以使用三个引号

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

前两个建议似乎来自 PEP 8。请注意,对于多行注释,PEP 8 告诉我们要从连续的单行注释构建它们,而不是作为多行字符串:https://www.python.org/dev/peps/pep-0008/#block-comments。 - Georgy

3
在Python 2.7.13中:
单个(Single):
"A sample single line comment "

多行文本:

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

2
你是说在Python 2.7中单引号可以创建一个注释? - mcalex
3
使用一组引号可以创建一个字符串。单行注释应该以 # 作为前缀。 - johanno

3
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)

你好

世界

使用三个双引号将文本括起来以支持文档字符串。

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

2

是的,同时使用两者是可以的:

最初的回答

'''
Comments
'''

并且

"""
Comments
"""

然而,在IDE中运行时,您需要记住的唯一一件事是必须“运行”整个文件才能被接受为多行代码。逐行“运行”将无法正常工作并显示错误。

原始答案:最初的回答


2
使用PyCharm IDE。
您可以使用Ctrl+/注释或取消注释代码行。Ctrl+/将当前行或几个选定行使用单行注释(在Django模板中为{#,在Python脚本中为#)进行注释或取消注释。
在Django模板中,按下Ctrl+Shift+/会将选择的源代码块用{% comment %}和{% endcomment %}标签括起来。
n = 5
while n > 0:
    n -= 1
    if n == 2:
        break
    print(n)

print("Loop ended.")

选中所有行,然后按Ctrl + /



最初的回答
# n = 5
# while n > 0:
#     n -= 1
#     if n == 2:
#         break
#     print(n)

# print("Loop ended.")

1

是的,您可以简单地使用

'''
Multiline!
(?)
'''

或者

"""
Hello
World!
"""

奖励: 这有点困难,但在旧版本中使用print函数或GUI更加安全:

# This is also
# a multiline comment.

对于这个问题,你可以选择你想要评论的文本并按下 Ctrl /(或者 /),在 PyCharmVS Code 中都适用。

但是你也可以编辑它们。例如,你可以将快捷键从Ctrl /更改为Ctrl Shift C

警告!

  1. 请注意,不要覆盖其他的快捷键!
  2. 注释必须正确缩进!

希望这个回答能有所帮助。祝你下次写回答好运!


1

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

移动到注释区域第一行的开头。

按下Ctrl+V进入可视模式。

使用箭头键选择要注释的所有行。

按下Shift+I。

按下#(或Shift+3)。

按下Esc。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接