我非常想要有以下行为:
def foo(bar=None):
if bar:
return other_function(other_thing[bar])
else:
for i in other_thing:
yield other_function(i)
Run Code Online (Sandbox Code Playgroud)
这个想法是该函数可以用作生成器来构建所有实例,或者它可以用于返回特定实例.这是在Python中执行此操作的好方法吗?如果没有,是否有更好的方法.
python ×1