相关疑难解决方法(0)

在python中混合yield和return语句是一种好习惯吗?

我非常想要有以下行为:

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

6
推荐指数
1
解决办法
3349
查看次数

标签 统计

python ×1