[Python-ideas] for/else syntax

Ron Adam rrr at ronadam.com
Sat Oct 3 17:27:45 CEST 2009



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




More information about the Python-ideas mailing list