I have a generator function generate
which yields 5 random numbers one at a time. I need to be able to generate the numbers in two ways:
you should use itertools.chain.from_iterable
return itertools.chain.from_iterable(generate(value) for value in values)
You can specify generator delegation using yield from
starting with python-3.3+.
def get_resource(values=None):
def resource_generator():
if values:
for value in values:
yield from generate(value)
else:
yield from generate(None)
return resource_generator()
Now,
>>> list(get_resource([1, 2, 3]))
[46, 99, 97, 1, 42, 79, 69, 9, 45, 25, 77, 56, 54, 7, 41]