Skip to content Skip to sidebar Skip to footer

Conditionally Yield A Generator In Python

Let's say I have this code: def f(data, all_at_once): if all_at_once: return data else: yield from data f([1,2,3], True) f always returns a generator, reg

Solution 1:

Here's a start. Perhaps it can be improved by a lambda

def gen(data):
    yield from data

def f(data, all_at_once):
    if all_at_once:
        return data
    return gen(data)

f([1,2,3], True)

Post a Comment for "Conditionally Yield A Generator In Python"