PEP on breaking outer loops with StopIteration

Paul Hankin paul.hankin at gmail.com
Mon Jun 9 22:39:07 EDT 2008


On Jun 10, 10:07 am, "Kris Kowal" <kris.ko... at cixar.com> 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 http://www.python.org/dev/peps/pep-3136/

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