The return value is not ignored, but the generators give only the values, and return just ends the generator, in this case earlier. In this case, advancing the generator never reaches the yield .
Whenever an iterator reaches the "end" of values ββto get, a StopIteration must be raised. Generators are no exception. Starting in Python 3.3, any return becomes an exception value:
>>> def gen(): ... return 3 ... yield 2 ... >>> try: ... next(gen()) ... except StopIteration as ex: ... e = ex ... >>> e StopIteration(3,) >>> e.value 3
Use the next() function to move iterators instead of directly calling .__next__() :
print(next(x))
Martijn Pieters May 27 '13 at 20:18 2013-05-27 20:18
source share