PEP on breaking outer loops with StopIteration

Luis Zarrabeitia kyrie at uh.cu
Tue Jun 10 01:29:43 EDT 2008


Quoting Kris Kowal <kris.kowal at cixar.com>:

> 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()
> 

I must say, I don't even like the idea of having a 'break', but I kind of like
this proposal.

However, it may be ambiguous [is that a word?] if the outer and inner for loop 
over the same object. Weird/unlikely situation, I know... but so is having a
deep break :D.

-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie





More information about the Python-list mailing list