How to indent Python list-comprehensions?

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

    Where I work, our coding guidelines would have us do something like this:

    all_posts_uuid_query = self.db.query(schema.allPostsUuid)
    all_posts_uuid_list = all_posts_uuid_query.execute(timeout=20)
    all_uuid_list = [
        x.id 
        for x in all_posts_uuid_list 
        if (
            x.type == "post" 
            and 
            not x.deleted  # <-- if you don't care about NULLs / None
        )
    ]
    

提交回复
热议问题