如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
当前回答
你可以使用下面的方法。这叫做DockString。
def my_function(arg1):
"""
Summary line.
Extended description of function.
Parameters:
arg1 (int): Description of arg1
Returns:
int: Description of return value
"""
return arg1
print my_function.__doc__
其他回答
没有多行注释这样的特性。#是注释一行代码的唯一方法。 你们中的许多人回答“一个评论”,这是他们的解决方案。
这看起来是可行的,但在Python内部,“'接受作为常规字符串封装的行,解释器不会像使用#的注释那样忽略它。
点击这里查看官方文件
如果你写了评论
"""
long comment here
"""
在脚本中间,Python/linters不会识别它。折叠将是混乱的,因为上面的注释不是标准建议的一部分。用起来更好
# Long comment
# here.
如果你使用Vim,你可以添加像评论这样的插件。通过按Vjgcc自动注释掉长行注释。其中Vj选择了两行代码,gcc将它们注释掉。
如果你不想使用像上面这样的插件,你可以使用搜索和替换
:.,.+1s/^/# /g
这将用#替换当前和下一行的第一个字符。
我建议不要在多行注释中使用“”!
这里有一个简单的例子来强调可能被认为是意外的行为:
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
选择要注释的行,然后使用Ctrl + ?在Sublime文本编辑器中注释或取消注释Python代码。
对于单行,可以使用Shift + #。
在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
"""