"also" to balance "else" ?

Fredrik Lundh fredrik at pythonware.com
Wed Jun 15 04:57:30 EDT 2005


Ron Adam wrote:

> So the (my) confusion comes from the tendency to look at it in terms of
> overall program flow rather than in terms of the specific conditional
> logic.
>
> In a for loop the normal, as in terminating normally, behavior of a loop
> is one where the loop test evaluates as 'False' ending the loop.  And
> the abnormal or counter behavior is when a break statement executes.
> Thus the 'else' block is the normal result, and the skipping the 'else'
> block becomes the abnormal counter behavior.

a typical use-case for for-in-else is a search loop:

    for item in collection:
        if predicate(item):
            print "found", item
            break
    else:
        print "not found"
        return

    print "use", item

where your "abnormal behaviour" is, of course, the expected
behaviour.  if you insist on looking at things the wrong way,
things will look reversed.

</F>






More information about the Python-list mailing list