for-else

Sion Arrowsmith siona at chiark.greenend.org.uk
Fri Mar 7 07:32:47 EST 2008


Jeffrey Barish  <jeff_barish at earthlink.net> wrote:
>Terry Reedy wrote:
>> A for-loop is equivalent to a while loop with the condition 'iterator is
>> not exhausted'.  So do_else when that condition is false -- the iterator
>> is exhausted.
>I think that this is the most important statement in this thread.  As others
>have expressed, I too found for-else surprising when I first encountered
>it.  It made sense to me when I analogized for with if: [ ... ]

And to pull those two together, I found while-else comprehensible by
analogy with if-else:

while stmt:
    do_something() # stmt is True
else:
    do_something_else() # stmt is False

I don't think I've ever used a for-else in the wild, but I have used
while-else. And knowing how that works, it's kind of obvious what
for-else does.

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
        -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list