如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
当前回答
AFAIK, Python没有块注释。对于注释单个行,可以使用#字符。
如果你正在使用notepad++,有一个块注释的快捷方式。我相信其他的像gVim和Emacs也有类似的特性。
其他回答
好吧,你可以试试这个(当运行引号时,第一个问题的输入应该用'引号括起来):
"""
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)
"""之间的任何内容都将被注释。
如果你正在寻找单行注释,那么它是#。
如果你写了评论
"""
long comment here
"""
在脚本中间,Python/linters不会识别它。折叠将是混乱的,因为上面的注释不是标准建议的一部分。用起来更好
# Long comment
# here.
如果你使用Vim,你可以添加像评论这样的插件。通过按Vjgcc自动注释掉长行注释。其中Vj选择了两行代码,gcc将它们注释掉。
如果你不想使用像上面这样的插件,你可以使用搜索和替换
:.,.+1s/^/# /g
这将用#替换当前和下一行的第一个字符。
多行注释实际上在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表示取消注释。
如果在带有代码的一行中编写注释,则必须在#符号之前留下2个空格,在#符号之前留下1个空格
print("Hello World") # printing
如果在新行上写注释,必须写注释,在#符号中留下1个空格kn
# single line comment
要编写超过一行的注释,需要使用3个引号
"""
This is a comment
written in
more than just one line
"""
我建议不要在多行注释中使用“”!
这里有一个简单的例子来强调可能被认为是意外的行为:
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