[Python-ideas] for/else syntax

Ron Adam rrr at ronadam.com
Sat Oct 3 18:19:17 CEST 2009

Yuvgoog Greenle wrote:
> Document it as much as you want but the "else" doesn't hint or clue as
> to whether it catches breaks or is skipped by them.
> The next problem's gonna be at midnight when you're reading code and
> asking yourself "hmmm, for, blah blah, else, hmm oh yeah, else-break,
> so this else occurs whenever there's a break."

True, and that is part of the other 20% I expect the documentation would't 

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...

> On Sat, Oct 3, 2009 at 6:27 PM, Ron Adam <rrr at ronadam.com> wrote:
>> Nick Coghlan wrote:
>>> Ron Adam wrote:
>>>> Nick Coghlan wrote:
>>>>> Nick Coghlan wrote:
>>>>> For a loop with no break statements the else clause will *always* be
>>>>> executed, hence using the clause is completely redundant - the code it
>>>>> contains might as well be dedented and placed inline at the same level
>>>>> as the loop header.
>>>> The loop could have a "return", "raise", or be inside a "try/except"
>>>> instead of having a "break".
>>> As I've said elsewhere in this thread, while the else clause won't be
>>> executed in those cases, neither will any code that occurs immediately
>>> after the loop statement. Accordingly, using the else clause is
>>> unnecessary and rather misleading - just writing the extra code after
>>> the loop would be much clearer.
>>> The number one thing I have learned from this thread is the fact that
>>> the else clause only makes sense when used in conjunction with a break
>>> statement and the fact that this construct isn't regularly documented
>>> and explained as for/break/else and while/break/else is the major
>>> barrier to understanding what it does.
>>> Cheers,
>>> Nick.
>> Ahh, Ok, I agree.  Documenting them as for/break/else and while/break/else
>> would probably reduce 80% of the confusion people have with it.
>> Ron
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/mailman/listinfo/python-ideas

More information about the Python-ideas mailing list