[Python-ideas] Map and filter should also convert StopIteration to RuntimeError

Oscar Benjamin oscar.j.benjamin at gmail.com
Fri Dec 12 22:44:16 CET 2014

On 12 December 2014 at 21:24, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Fri, Dec 12, 2014 at 4:14 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>> I propose that map.__next__ convert StopIteration raised by func to
>> RuntimeError
> Aren't we stepping on a slippery slope here?  What about say
>>>> def f(x, y):
> ...     if x > 2:
> ...         raise StopIteration
> ...     return x + y
> ...
>>>> import itertools
>>>> itertools.accumulate(range(10), f)
> <itertools.accumulate object at 0x10acc0fc8>
>>>> list(_)
> [0, 1, 3]

It applies to map, filter, and 5 or 6 things in itertools and
certainly many other things outside stdlib.

I guess the question is: should it be considered a bug for an iterator
to leak a StopIteration outside of its "normal" exit condition?
Apparently it was for generators which represent the majority of all
iterators and it was considered sufficiently important to change the
core language in a backward incompatible way.


More information about the Python-ideas mailing list