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

Oscar Benjamin oscar.j.benjamin at gmail.com
Fri Dec 12 23:11:25 CET 2014


On 12 December 2014 at 21:59, Chris Barker <chris.barker at noaa.gov> wrote:
> On Fri, Dec 12, 2014 at 1:44 PM, Oscar Benjamin <oscar.j.benjamin at gmail.com>
> wrote:
>>
>> 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
>
> I  don't think anyone aid it was a bug -- often it is a bug, but that's
> use-case dependent.

I don't think that the positive use-cases of this feature are common.

>> which represent the majority of all
>> iterators and it was considered sufficiently important to change the
>> core language in a backward incompatible way.
>
> It is considered prone to hard to find and understand bugs -- which was the
> motivation for the PEP.

The same reasoning applies here. I had a bug like this and it was
data-dependent, hard to reproduce, and there was no exception to hook
a debugger onto or stack trace to give information. Since the full
program was quite slow and I had to keep running it in full it turned
what would have been a 1 minute fix into a half of a day of debugging.
Since then I've simply been wary about next() and haven't suffered the
problem again. I suspect that in the same situation now it wouldn't
take me so long because at the time I wasn't really aware of the
possibility that StopIteration could get "caught" by a for-loop
several frames up.


More information about the Python-ideas mailing list