如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
当前回答
使用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.")
其他回答
在Python 2.7中,多行注释是:
"""
This is a
multilline comment
"""
如果你在一个类中,你应该正确地制表。
例如:
class weather2():
"""
def getStatus_code(self, url):
world.url = url
result = requests.get(url)
return result.status_code
"""
从公认的答案…
你可以使用三引号字符串。当它们不是文档字符串(类/函数/模块中的第一件事)时,它们将被忽略。
这是不正确的。与注释不同,三引号字符串仍然被解析,并且必须在语法上有效,无论它们出现在源代码中的哪个位置。
如果你试图运行这段代码…
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()
没有多行注释这样的特性。#是注释一行代码的唯一方法。 你们中的许多人回答“一个评论”,这是他们的解决方案。
这看起来是可行的,但在Python内部,“'接受作为常规字符串封装的行,解释器不会像使用#的注释那样忽略它。
点击这里查看官方文件
在windows中:你也可以选择文本或代码块,按CTR + /,如果你想删除注释,也可以这样做。 在mac中:它应该是comment + /
是的,两者都可以使用:
'''
Comments
'''
and
"""
Comments
"""
但是,在IDE中运行时,你需要记住的唯一一件事是,你必须“运行”整个文件,以接受多行代码。逐行“RUN”将无法正常工作,并将显示错误。