1.5.2 for: else:

William Tanksley wtanksle at dolphin.openprojects.net
Tue Jul 27 17:17:08 EDT 1999


On Tue, 27 Jul 1999 14:29:43 +0200, Thomas Wouters wrote:
>On Tue, Jul 27, 1999 at 04:15:44PM +0400, Oleg Broytmann wrote:

>>    The following program:
>> ----------
>> for a in ['a', 12]:
>>    print a
>> else:
>>    print "Empty!"
>> ----------

>http://www.python.org/doc/current/tut/node6.html#SECTION006200000000000000000

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

Understanding the current semantics seems to require understanding the
nature of loop tests in Python.  Since I understand them I can agree that
this makes sense, but I'm still a bit leery, because you're not 'elsing'
on the data but rather on code inside the loop.

I would _definitely_ use exceptions to handle this.

>Thomas Wouters <thomas at xs4all.net>

-- 
-William "Billy" Tanksley




More information about the Python-list mailing list