假设我有这样的代码:复杂的嵌套列表内涵,PEP8
questions = [{
"title": q.title,
"votes": q.get_total_votes(),
"options": [{
"title": o.title,
"votes": o.votes
} for o in q.get_options()]
} for q in queue.get_questions()]
但大多数PEP8例子说我应该写这样的:
questions = [{"title": q.title,
"votes": q.get_total_votes(),
"options": [{"title": o.title, "votes": o.votes}
for o in q.get_options()]}
for q in queue.get_questions()]
是第二个版本preferrable /更具可读性?或者,也许列表内涵是不是要做到这一点的最好办法,我应该做纯
questions = []
for q in queue.get_questions():
options = []
for o in q.get_options():
options.append({"title": o.title, "votes": o.votes})
questions.append({"title": q.title,
"votes": q.get_total_votes(),
"options": options})