Dun*_*ter 1081 python documentation comments python-2.7 python-3.x
我最近开始学习Python,但我找不到如何实现多行注释.大多数语言都有块注释符号
/*
*/
Run Code Online (Sandbox Code Playgroud)
我在Python中试过这个,但它会抛出一个错误,所以这可能不是正确的方法.Python实际上是否具有多行注释功能?
Pet*_*rin 1691
您可以使用三引号字符串.当它们不是文档字符串(类/函数/模块中的第一件事)时,它们将被忽略.
'''
This is a multiline
comment.
'''
Run Code Online (Sandbox Code Playgroud)
(确保'''
适当地缩进前导以避免IndentationError
.)
Guido van Rossum(Python的创建者)在推文中称它为 "专业提示".
但是,Python的样式指南PEP8 倾向于使用连续的单行注释,这也是许多项目中的内容.编辑通常有一个快捷方式来轻松完成这项工作.
unu*_*tbu 78
从某种意义上说,Python确实有一个多行字符串/注释语法,除非用作文档字符串,否则多行字符串不会生成字节码 - 就像 - #
引用注释一样.实际上,它的行为与评论完全相同.
另一方面,如果你说这个行为必须在官方文档中记录为真正的注释语法,那么是的,你说这是不正确的,作为语言规范的一部分是正确的.
在任何情况下,您的编辑器也应该能够轻松地注释掉所选区域(通过#
单独放置在每条线的前面).如果没有,请切换到可执行的编辑器.
没有特定文本编辑功能的Python编程可能是一种痛苦的经历.找到正确的编辑器(并知道如何使用它)可以对Python编程体验的感知方式产生重大影响.
编辑器不仅能够注释掉所选区域,还应该能够轻松地向左右移动代码块,并且当您按Enter键时应自动将光标置于当前缩进级别.代码折叠也很有用.
为了防止链接衰减,这里是Guido van Rossum的推文的内容:
@BSUCSClub Python技巧:您可以使用多行字符串作为多行注释.除非用作文档字符串,否则它们不会生成代码!:-)
Aya*_*Aya 42
从接受的答案......
您可以使用三引号字符串.当它们不是文档字符串(类/函数/模块中的第一件事)时,它们将被忽略.
这是不正确的.与注释不同,三重引用的字符串仍然被解析,并且必须在语法上有效,无论它们出现在源代码中的何处.
如果您尝试运行此代码...
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
Run Code Online (Sandbox Code Playgroud)
你会得到......
ValueError: invalid \x escape
Run Code Online (Sandbox Code Playgroud)
...在Python 2.x或...上
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 79-80: truncated \xXX escape
Run Code Online (Sandbox Code Playgroud)
...在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()
Run Code Online (Sandbox Code Playgroud)
小智 34
在Python 2.7中,多行注释是:
"""
This is a
multilline comment
"""
Run Code Online (Sandbox Code Playgroud)
如果你在一个类中,你应该正确地选中它.
例如:
class weather2():
"""
def getStatus_code(self, url):
world.url = url
result = requests.get(url)
return result.status_code
"""
Run Code Online (Sandbox Code Playgroud)
我希望它有所帮助!
San*_*rma 24
AFAIK,Python没有块注释.要评论单个行,您可以使用该#
字符.
如果您使用的是Notepad ++,则可以使用块注释的快捷方式.我确信像gVim和Emacs这样的人也有类似的功能.
alp*_*989 10
如果你发表评论
"""
long comment here
"""
Run Code Online (Sandbox Code Playgroud)
在脚本中间,python/linters不会重新识别它.折叠将被搞砸,因为上述评论不是标准建议的一部分.它更好用
# long comment
# here.
Run Code Online (Sandbox Code Playgroud)
如果你使用vim
,你可以插件,如https://github.com/tpope/vim-commentary,通过按下自动注释掉长行注释Vjgcc
.当Vj
选择两行代码,并gcc
评论他们.
如果你不想使用上面的插件你可以使用搜索和替换之类的
:.,.+1s/^/# /g
.
这将替换当前和下一行的第一个字符#
.
小智 6
好吧,你可以尝试这个(运行引用时,第一个问题的输入应该用 引用'
):
"""
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)
Run Code Online (Sandbox Code Playgroud)
两者之间包含的任何内容都"""
将被注释。
如果您正在寻找单行注释,那么它就是#
.
小智 5
不幸的是,字符串化并不总是可以用作注释!因此,更安全的做法是坚持在每行前面加一个#
。
这是一个例子:
test1 = [1, 2, 3, 4,] # test1 contains 4 integers
test2 = [1, 2, '''3, 4,'''] # test2 contains 2 integers **and the string** '3, 4,'
Run Code Online (Sandbox Code Playgroud)
Python 中的多行注释:
对我来说,“”和“”都有效。
例子:
a = 10
b = 20
c = a+b
'''
print ('hello')
'''
print ('Addition is: ', a+b)
Run Code Online (Sandbox Code Playgroud)
例子:
a = 10
b = 20
c = a+b
"""
print('hello')
"""
print('Addition is: ', a+b)
Run Code Online (Sandbox Code Playgroud)
我建议不要使用"""
多行注释!
下面是一个简单的例子来突出可能被认为是意外行为的情况:
print('{}\n{}'.format(
'I am a string',
"""
Some people consider me a
multi-line comment, but
"""
'clearly I am also a string'
)
)
Run Code Online (Sandbox Code Playgroud)
现在看看输出:
I am a string
Some people consider me a
multi-line comment, but
clearly I am also a string
Run Code Online (Sandbox Code Playgroud)
多行字符串不被视为注释,而是与'clearly I'm 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'
)
)
Run Code Online (Sandbox Code Playgroud)
输出:
I am a string
clearly I am also a string
Run Code Online (Sandbox Code Playgroud)
如果在有代码的一行中写注释,则必须写注释,#号前留2个空格,#号前留1个空格
print("Hello World") # printing
Run Code Online (Sandbox Code Playgroud)
如果换行写注释,必须写注释,#号处留1个空格kn
# single line comment
Run Code Online (Sandbox Code Playgroud)
要编写超过 1 行的注释,请使用 3 个引号
"""
This is a comment
written in
more than just one line
"""
Run Code Online (Sandbox Code Playgroud)