本文翻译自:Way to create multiline comments in Python?
I have recently started studying Python , but I couldn't find how to implement multi-line comments. 我最近开始研究Python ,但是找不到如何实现多行注释。 Most languages have block comment symbols like 大多数语言都有块注释符号,例如
/*
*/
I tried this in Python, but it throws an error, so this probably is not the correct way. 我在Python中尝试过此方法,但它引发了错误,因此这可能不是正确的方法。 Does Python actually have a multiline comment feature? Python实际上是否具有多行注释功能?
#1楼
参考:https://stackoom.com/question/WIJw/在Python中创建多行注释的方法
#2楼
In Python 2.7 the multiline comment is: 在Python 2.7中,多行注释为:
"""
This is a
multilline comment
"""
In case you are inside a class you should tab it properly. 如果您在班级里,应该正确地选择它。
For example: 例如:
class weather2():
"""
def getStatus_code(self, url):
world.url = url
result = requests.get(url)
return result.status_code
"""
#3楼
From the accepted answer... 从接受的答案...
You can use triple-quoted strings. 您可以使用三引号引起来的字符串。 When they're not a docstring (first thing in a class/function/module), they are ignored. 如果它们不是文档字符串(类/函数/模块中的第一件事),则将其忽略。
This is simply not true. 这是不正确的。 Unlike comments, triple-quoted strings are still parsed and must be syntactically valid, regardless of where they appear in the source code. 与注释不同,三引号字符串仍然会被解析,并且在语法上必须有效,无论它们在源代码中的位置如何。
If you try to run this 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
You'll get either... 你会得到...
ValueError: invalid \x escape
...on Python 2.x or... ...在Python 2.x上...
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 79-80: truncated \xXX escape
...on Python 3.x. ...在Python 3.x上。
The only way to do multi-line comments which are ignored by the parser is... 进行解析器忽略的多行注释的唯一方法是...
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()
#4楼
On Python 2.7.13: 在Python 2.7.13上:
Single: 单:
"A sample single line comment "
Multiline: 多行:
"""
A sample
multiline comment
on PyCharm
"""
#5楼
Well, you can try this (when running the quoted, the input to the first question should quoted with '
): 好了,您可以尝试一下(在运行引号时,第一个问题的输入应用'
引号):
"""
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)
Whatever enclosed between """
will be commented. 无论在"""
之间包含什么内容,都将被评论。
If you are looking for single-line comments then it's #
. 如果您要查找单行注释,则为#
。
#6楼
If you put a comment in 如果您发表评论
"""
long comment here
"""
in the middle of a script, python/linters wont reccognize that. 在脚本中间,python / linters不会意识到这一点。 Folding will be messed up, as the above comment is not part of the standard recommendations. 折叠将被弄乱,因为以上注释不是标准建议的一部分。 Its better to use 更好用
# long comment
# here.
If you use vim
, you can plugins like https://github.com/tpope/vim-commentary , to automatically comment out long lines of comments by pressing Vjgcc
. 如果使用vim
,则可以通过按Vjgcc
这样的插件(例如https://github.com/tpope/vim-commentary )自动注释掉长行注释。 Where Vj
selects 2 lines of code, and gcc
comments them out. Vj
选择2行代码,然后gcc
其注释掉。
If you dont want to use plugins like the above you can use search and replace like 如果您不想使用上述插件,则可以使用搜索和替换类似
:.,.+1s/^/# /g
. :.,.+1s/^/# /g
。
This will replace the first character on the current and next line with #
. 这将用#
替换当前行和下一行的第一个字符。