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:
for x in range(5):
do_something()
else:
do_something_else()
means
do_something repeatedly when the condition (iterator not exhausted) is true
and do_something_else when the condition is not true, just as
if condition:
do_something()
else:
do_something_else()
means do_something once when the condition is true and do_something_else
when the condition is not true. I find it elegant that Python does not
introduce additional keywords to deal with situations that are comparable.
--
Jeffrey Barish
--
http://mail.python.org/mailman/listinfo/python-list