In Python 3, you can use yield from
to yield items from a subgenerator:
def find_stuff(visitor):
for x in (1, 2, 3, 4, 5):
yield from visitor(x)
In Python 2, you have to loop over the subgenerator. This takes more code and doesn't handle a few edge cases, but it's usually good enough:
def find_stuff(visitor):
for x in (1, 2, 3, 4, 5):
for item in visitor(x):
yield item
The edge cases are things like trying to send
values or throw
exceptions into the subgenerator. If you're not using coroutine functionality, you probably don't need to worry about them.