Often generator functions are simpler to write than iterators, e.g. consider this example generator function
def generator_fn():
x = 0
for y in range(10):
x += y
yield x
Is there a straightforward way to transform an arbitrary generator function into an iterator object, i.e.,
class IteratorFromGenerator(object):
def __init__(self, generator_fn):
self.generator_fn = generator_fn
def __iter__(self):
# ???
pass
def __next__(self):
# ???
pass
To me this seems favorable but impossible.
Favorable, because generator functions are often simpler to write and require less code. Usually, I begin writing generator functions and at some point, I need an iterator object (e.g. when generator_fn
is a member of my class Foo
and I want to write for x in my_foo
instead of for x in my_foo.generator_fn()
.
Impossible, because generator functions define the execution flow of the loop process, while iterator objects simply offer their __next__
interface to be requested by an undefined "outer" execution flow.
Am I right or is there a possibility to do this?
Of course, apart from the trivial, memory-consuming solution of consuming all generated elements into a list.
Thanks for any help.