Line continuation for list comprehensions or generator expressions in python

前端 未结 3 1731
天命终不由人
天命终不由人 2020-12-02 11:29

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_pret         


        
3条回答
  •  無奈伤痛
    2020-12-02 12:20

    I'm not opposed to:

    variable = [something_that_is_pretty_long
                for something_that_is_pretty_long
                in somethings_that_are_pretty_long]
    

    You don't need \ in this case. In general, I think people avoid \ because it's slightly ugly, but also can give problems if it's not the very last thing on the line (make sure no whitespace follows it). I think it's much better to use it than not, though, in order to keep your line lengths down.

    Since \ isn't necessary in the above case, or for parenthesized expressions, I actually find it fairly rare that I even need to use it.

提交回复
热议问题