[Python-ideas] Propagating StopIteration value
Oscar Benjamin
oscar.j.benjamin at gmail.com
Tue Oct 9 01:59:26 CEST 2012
On 9 October 2012 00:47, Guido van Rossum <guido at python.org> wrote:
>
> Given the elusive nature of StopIteration (many operations catch and
> ignore it, and that's the main intended use) I don't think it should
> be used to pass along *important* information except for the specific
> case of coroutines, where the normal use case is to use .send()
> instead of .__next__() and to catch the StopIteration exception.
>
It certainly is elusive! I caught a bug a few weeks ago where
StopIteration was generated from a call to next and caught by a for
loop several frames above. I couldn't work out why the loop was
terminating early (since there was no attempt to catch any exceptions
anywhere in the code) and it took about 20 minutes of processing to
reproduce. With no traceback and no way to catch the exception with
pdb it had me stumped for a while.
Oscar
More information about the Python-ideas
mailing list