[Python-ideas] Propagating StopIteration value

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Oct 7 04:11:54 CEST 2012


Steven D'Aprano wrote:

> py> def myiter():
> ...     yield 1
> ...     raise StopIteration("spam")
> ...
> py> it = map(lambda x:x, myiter())
> py> next(it)
> 1
> py> next(it)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> StopIteration
> 
> The argument given to StopIteration is eaten by map.

It's highly debatable whether this is even wrong. The purpose
of StopIteration(value) is for a generator to return a value
to its immediate caller when invoked using yield-from. The
value is not intended to propagate any further than that.

A non-iterator analogy would be

    def f():
       return 42

    def g():
       f()

Would you expect g() to return 42 here?

-- 
Greg



More information about the Python-ideas mailing list