PEP on breaking outer loops with StopIteration

Paul Hankin paul.hankin at
Tue Jun 10 04:39:07 CEST 2008

On Jun 10, 10:07 am, "Kris Kowal" <kris.ko... at> wrote:
> I had a thought that might be pepworthy.  Might we be able to break
> outer loops using an iter-instance specific StopIteration type?
> This is the desired, if not desirable, syntax::
>     import string
>     letters = iter(string.lowercase)
>     for letter in letters:
>         for number in range(10):
>             print letter, number
>             if letter == 'a' and number == 5:
>                 raise StopIteration()
>             if letter == 'b' and number == 5:
>                 raise letters.StopIteration()

Have you checked out

It contains exactly this idea, but using 'break letters' rather than
'raise letters.StopIteration()'. I think I like the PEP's syntax
better than yours, but anyway, it was rejected.

Paul Hankin

More information about the Python-list mailing list