如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
当前回答
没有多行注释这样的特性。#是注释一行代码的唯一方法。 你们中的许多人回答“一个评论”,这是他们的解决方案。
这看起来是可行的,但在Python内部,“'接受作为常规字符串封装的行,解释器不会像使用#的注释那样忽略它。
点击这里查看官方文件
其他回答
多行注释实际上在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表示取消注释。
是的,你可以简单地使用
'''
Multiline!
(?)
'''
or
"""
Hello
World!
"""
好处:它有点难,但在旧版本、打印函数或gui中使用更安全:
# This is also
# a multiline comment.
对于这个,你可以选择你想要注释的文本,并按Ctrl /(或⌘/),在PyCharm和VS Code中。
但是你可以编辑它们。例如,可以更改快捷键“Ctrl /” 按Ctrl Shift C。
警告!
小心,不要覆盖其他快捷方式! 注释必须正确缩进!
希望这个回答能有所帮助。祝你下次写出其他答案时好运!
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 ide都有一个短键来“注释掉”多行代码。
从公认的答案…
你可以使用三引号字符串。当它们不是文档字符串(类/函数/模块中的第一件事)时,它们将被忽略。
这是不正确的。与注释不同,三引号字符串仍然被解析,并且必须在语法上有效,无论它们出现在源代码中的哪个位置。
如果你试图运行这段代码…
def parse_token(token):
"""
This function parses a token.
TODO: write a decent docstring :-)
"""
if token == '\\and':
do_something()
elif token == '\\or':
do_something_else()
elif token == '\\xor':
'''
Note that we still need to provide support for the deprecated
token \xor. Hopefully we can drop support in libfoo 2.0.
'''
do_a_different_thing()
else:
raise ValueError
你会得到…
ValueError: invalid \x escape
...Python 2。x或…
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 79-80: truncated \xXX escape
...Python 3.x。
做被解析器忽略的多行注释的唯一方法是…
elif token == '\\xor':
# Note that we still need to provide support for the deprecated
# token \xor. Hopefully we can drop support in libfoo 2.0.
do_a_different_thing()