decorating a function that yields
A generator function, when called, returns an iterator object. If your decorator is itself a generator too, you'll need to loop over the wrapped result:
def foobar_creator(func):
def wrapped(**kwargs):
gen = func(**kwargs)
flag = True
for k, v in kwargs:
if res % v == 0:
flag = False
yield k
if flag:
for res in gen:
yield res
return wrapped
If you are using Python 3.3 or up, you can use delegation to hand control the wrapped generator, by using yield from
:
if flag:
yield from gen
Instead of yielding every potential return value, why not yield only those that actually exist? Something like
def wrap(f, arg):
for x in f(arg):
yield x
(actual decorator syntax, handling of positional and keyword arguments, etc. is omitted for clarity.)
For the case in comment42684128, the solution is as simple as:
(v for v in f(<args>) if filter_condition(v))
As a decorator:
def yfilter(filter_condition):
def yfilter_p(f):
def wrapped(*args,**kwargs):
return (v for v in f(*args,**kwargs) if filter_condition(v))
return wrapped
return yfilter_p