[Python-ideas] for/else syntax

Carl Johnson cmjohnson.mailinglist at gmail.com
Sat Oct 3 08:26:57 CEST 2009


Steven D'Aprano:

> You can already do this today, with no change to the compiler, using
> only a slightly different syntax to that suggested:
>
> for i, j in enumerate(something):
>    # suite
>    i += 1
> else:  # if not break
>    i = 0
>
>
> Best of all, it's an extensible syntax, so there's no need for
> bike-shedding arguments about whether it should be spelled "not break"
> or "no break" or "didn't break" or "only if loop ended normally". All
> of these variants, and more, are accepted:

Hoisted by my own petard! :-D This was also my suggestion for adding a
do-while to Python a few weeks back:

while True: #do {
   stuff
# } until {
   if cond: break # }

— Carl



More information about the Python-ideas mailing list