如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
当前回答
我建议不要在多行注释中使用“”!
这里有一个简单的例子来强调可能被认为是意外的行为:
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
其他回答
Python 2.7.13:
单:
"A sample single line comment "
多行:
"""
A sample
multiline comment
on PyCharm
"""
在其他答案中,我发现最简单的方法是使用IDE注释函数,该函数使用Python注释支持#。
我正在使用Anaconda Spyder,它有:
Ctrl + 1 -注释/取消注释 Ctrl + 4 -注释代码块 Ctrl + 5 -取消注释代码块
它可以用#注释/取消注释一行/多行代码。
我觉得这是最简单的。
例如,一个块注释:
# =============================================================================
# Sample Commented code in spyder
# Hello, World!
# =============================================================================
Python中的内嵌注释以哈希字符开始。
hello = "Hello!" # This is an inline comment
print(hello)
你好!
注意,字符串文字中的哈希字符只是一个哈希字符。
dial = "Dial #100 to make an emergency call."
print(dial)
拨打100拨打紧急电话。
散列字符也可以用于单行或多行注释。
hello = "Hello"
world = "World"
# First print hello
# And print world
print(hello)
print(world)
你好 世界
用三双引号将文本括起来以支持docstring。
def say_hello(name):
"""
This is docstring comment and
it's support multi line.
:param name it's your name
:type name str
"""
return "Hello " + name + '!'
print(say_hello("John"))
你好约翰!
在文本中加上三个单引号作为块注释。
'''
I don't care the parameters and
docstrings here.
'''
是的,两者都可以使用:
'''
Comments
'''
and
"""
Comments
"""
但是,在IDE中运行时,你需要记住的唯一一件事是,你必须“运行”整个文件,以接受多行代码。逐行“RUN”将无法正常工作,并将显示错误。
我建议不要在多行注释中使用“”!
这里有一个简单的例子来强调可能被认为是意外的行为:
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