How to indent Python list-comprehensions?

前端 未结 7 1260
失恋的感觉
失恋的感觉 2021-01-30 06:17

List comprehensions can be useful in certain situations, but they can also be rather horrible to read.. As a slightly exaggerated example, how would you indent the following?

7条回答
  •  后悔当初
    2021-01-30 06:58

    For me that's too much. Maybe it's just a terrible example, since "type" and "deleted" would clearly be part of the db query.

    I tend to think that if a list comprehension spans multiple lines it probably shouldn't be a list comprehension. Having said that, I usually just split the thing at "if" like other people have and will answer here.

提交回复
热议问题