【发布时间】:2014-10-06 19:32:02
【问题描述】:
我在一个函数中有很多连续的语句,如下所示:
if condition1:
yield x
else:
yield None
if condition2:
yield y
else:
yield None
...
有没有办法让这种代码更简洁?
【问题讨论】:
-
yield x if condition1 else None? -
你能举例说明你有什么样的条件吗?否则我能想到的就是让它成为一个班轮:
yield x if condition1 else None -
删除了 switch-statement 标签,因为 Python 没有 switch 语句,如果有的话,我无法想象你会如何期望在这里为你提供帮助。
标签: python if-statement yield