如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
当前回答
Python 2.7.13:
单:
"A sample single line comment "
多行:
"""
A sample
multiline comment
on PyCharm
"""
其他回答
如果你写了评论
"""
long comment here
"""
在脚本中间,Python/linters不会识别它。折叠将是混乱的,因为上面的注释不是标准建议的一部分。用起来更好
# Long comment
# here.
如果你使用Vim,你可以添加像评论这样的插件。通过按Vjgcc自动注释掉长行注释。其中Vj选择了两行代码,gcc将它们注释掉。
如果你不想使用像上面这样的插件,你可以使用搜索和替换
:.,.+1s/^/# /g
这将用#替换当前和下一行的第一个字符。
使用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.")
你可以使用三引号字符串。当它们不是docstring(类/函数/模块中的第一件事)时,它们将被忽略。
'''
This is a multiline
comment.
'''
(确保适当缩进前导" '以避免出现IndentationError。)
Guido van Rossum (Python的创造者)在推特上发布了这条“专业提示”。
然而,Python的风格指南PEP8倾向于使用连续的单行注释,像这样:
# This is a multiline
# comment.
...这也是你在很多项目中会发现的。文本编辑器通常有一个快捷方式来轻松做到这一点。
在windows中:你也可以选择文本或代码块,按CTR + /,如果你想删除注释,也可以这样做。 在mac中:它应该是comment + /
我建议不要在多行注释中使用“”!
这里有一个简单的例子来强调可能被认为是意外的行为:
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
多行字符串不被视为注释,但它与'显然我也是一个字符串'连接起来,形成一个单一的字符串。
如果您想注释多行,请根据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