在python中列表推导或生成器表达式的行连续 [英] Line continuation for list comprehensions or generator expressions in python
本文介绍了在python中列表推导或生成器表达式的行连续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty_long]
我也看过不喜欢使用'\'分隔行的人,
从来没有理解为什么。这是什么原因?
解决方案
[x
for
x
in
(1,2,3)
]
工作正常,所以你可以几乎做你喜欢。我个人宁愿
[something_that_is_pretty_long
for something_that_is_pretty_long
in somethings_that_are_pretty_long]
之所以不太赞赏 \
出现在一行的 end ,它或者不突出或需要额外的填充,当行长度改变时必须固定:
x = very_long_term \
+ even_longer_term_than_the_previous \
+ a_third_term
在这种情况下,使用括号:
x =(very_long_term
+ even_longer_term_than_the_previous
+ a_third_term)
How are you supposed to break up a very long list comprehension?
[something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty_long]
I have also seen somewhere that people that dislike using '\' to break up lines, but never understood why. What is the reason behind this?
解决方案
[x
for
x
in
(1,2,3)
]
works fine, so you can pretty much do as you please. I'd personally prefer
[something_that_is_pretty_long
for something_that_is_pretty_long
in somethings_that_are_pretty_long]
The reason why \
isn't appreciated very much is that it appears at the end of a line, where it either doesn't stand out or needs extra padding, which has to be fixed when line lengths change:
x = very_long_term \
+ even_longer_term_than_the_previous \
+ a_third_term
In such cases, use parens:
x = (very_long_term
+ even_longer_term_than_the_previous
+ a_third_term)
这篇关于在python中列表推导或生成器表达式的行连续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文