1.5.2 for: else:

Gordon McMillan gmcm at hypernet.com
Tue Jul 27 19:35:10 EDT 1999


William Tanksley wrote:
> On Tue, 27 Jul 1999 14:29:43 +0200, Thomas Wouters wrote:

> >4.4 break and continue Statements, and else Clauses on Loops 
> 
> >Loop statements may have an else clause; it is executed when the loop
> >terminates through exhaustion of the list (with for) or when the condition
> >becomes false (with while), but not when the loop is terminated by a break
> >statement. 
> 
> Well, I'm suprised.  I had also expected the else clause to run on
> empty loop.  It seems so natural, in line with the meaning of a loop
> (loop on this; otherwise do that).
> 
> Fortunately I've never actually used it and thus been disenchanted.

Maybe it violated your expectations, but it's much more valuable than 
what you expected. After all, testing for an empty sequence is a 
no-brainer. But finding a match in a list, and testing whether you 
fell off the end without finding one, is (without the else clause) a 
much messier proposal.

your-disenchantment-is-my-pleasure-<wink>-ly y'rs



- Gordon




More information about the Python-list mailing list