[Python-ideas] for/else syntax

Ron Adam rrr at ronadam.com
Sat Oct 3 18:51:25 CEST 2009

Yuvgoog Greenle wrote:
> On Sat, Oct 3, 2009 at 7:19 PM, Ron Adam <rrr at ronadam.com> wrote:
>> True, and that is part of the other 20% I expect the documentation would't
>> help.
>> The only solution to that that I can think of is to change the esle's in for
>> and while loops to for/then and while/then in Python 5.0.
>> It might be doable, and if enough people want it, then just maybe...
> Documenting this syntax with the appropriate warnings and clear
> explanations is very important.
> But I don't think the amount of code mishaps on this thread are
> properly addressed just by better documentation. The syntax I proposed
> is harmless, it won't break any existing code, and it's something
> people in python 3 can use right now. Deprecation etc can wait for
> python 4. Don't tea5e me :)

Lol, Sorry about that. ;-),  but I do think it's too late to change 
for/else and while/else to for/then and while/then in python 4.0 already.

> Of course people here would have to say if my idea sounds good or not.
> while still_more_items():
>     if found_it():
>         break
> if not break:
>     # exhausted search space

I suppose most people won't like the way break is used in two entirely 
different ways depending on where it is.

When you see a break, two things become really clear:

    1: you are inside a loop

    2: we are exiting a loop

When you use it in the other context it makes those things a bit less 
obvious especially if you consider loops inside of loops.  We then have to 
consider the closeness these too statements are to each other.

    if not condition: break

    if not break: condition

we aren't exiting a loop in the second case, even though it may be inside 
another loop.


More information about the Python-ideas mailing list