Python:'#'后面加注反斜杠 [英] Python: '#' Comments after backslash
问题描述
这不起作用:
something = \
line_of_code * \ # Comment
another_line_of_code * \ # Comment
and_another_one * \ # Comment
etc
这也不是:
something = \
# Comment \
line_of_code * \
# Comment \
another_line_of_code * ...
/ p>
Neither does this:
something = \
''' Comment ''' \
line_of_code * \
''' Comment ''' \
another_line_of_code * ...
有办法在代码中将注释分成多行?
If there a way to make comments in the code broken into multiple lines?
推荐答案
这样做:
a, b, c, d = range(1, 5)
result = (
# First is 1
a *
# Then goes 2, result is 2 now
b *
# And then 3, result is 6
c *
# And 4, result should be 24
d
)
其实,根据PEP8 当将某个内容分成多行时,括号优先于斜杠:
Actually, according to PEP8 parentheses are preferred over slashes, when breaking something into multiple lines:
包装长线的首选方法是使用括号,括号和大括号中的Python隐式线连续。通过在括号中包装表达式,可以在多行上划分长线。
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
在你的情况下,它也允许放置注释。
In your case it also allows to put comments.
这是一个证明,它的工作原理: http://ideone.com/FlccUJ
Here is a proof, that it works: http://ideone.com/FlccUJ
这篇关于Python:'#'后面加注反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!