I am using python 3.5. When I tried to return a generator function instance and i am getting a StopIteration error. Why?
here is my code:
>>> def gen(start, end):
... '''generator function similar to range function'''
... while start <= end:
... yield start
... start += 1
...
>>> def check(ingen, flag=None):
... if flag:
... for n in ingen:
... yield n*2
... else:
... return ingen
...
>>> # Trigger else clause in check function
>>> a = check(gen(1,3))
>>> next(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration: <generator object gen at 0x7f37dc46e828>
It looks like the generator is somehow exhausted before the else clause is returns the generator.
It works fine with this function:
>>> def check_v2(ingen):
... return ingen
...
>>> b = check_v2(gen(1, 3))
>>> next(b)
1
>>> next(b)
2
>>> next(b)
3