How to indent Python list-comprehensions?

前端 未结 7 1262
失恋的感觉
失恋的感觉 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:59

    If you're set on a comprehension orestis's answer is good.

    For more complex comprehensions like that I'd suggest using a generator with yield:

    allUuids = list(self.get_all_uuids())
    
    
    def get_all_uuids(self):
        for x in self.db.query(schema.allPostsUuid).execute(timeout = 20):
            if x.type == "post" and x.deleted is not False:
                yield x.id
    

提交回复
热议问题