[Python-ideas] Propagating StopIteration value
Serhiy Storchaka
storchaka at gmail.com
Tue Oct 9 17:28:14 CEST 2012
On 09.10.12 16:07, Oscar Benjamin wrote:
> I really should have checked this before posting but I didn't have
> Python 3.3 available:
Generator expression also eats the StopIteration value:
>>> next(x for x in f())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
> These next two seem wrong since there are two iterables (but I don't
> think they can be done differently):
>
>>>> def g():
> .... return 'From the other generator...'
> .... yield
> ....
>>>> next(itertools.compress(f(), g()))
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> StopIteration: Returned from generator!
>>>> next(zip(f(), g()))
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> StopIteration: Returned from generator!
>>> def h():
... yield 42
... return 'From the another generator...'
...
>>> next(zip(f(), h()))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration: Returned from generator!
>>> next(zip(h(), f()))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration: Returned from generator!
This is logical. Value returned from the first exhausted iterator.
> I guess this should be treated as undefined behaviour? Perhaps it
> should be documented as such so that anyone who chooses to rely on it
> was warned.
This should be treated as implementation details now.
More information about the Python-ideas
mailing list