[Python-ideas] for/else syntax

Nick Coghlan ncoghlan at gmail.com
Sat Oct 3 17:12:51 CEST 2009


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.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list