"for" with "else"?
aahz at pythoncraft.com
Thu Sep 25 22:42:15 CEST 2003
In article <vn67fhhom95896 at corp.supernews.com>,
Michael Geary <Mike at DeleteThis.Geary.com> wrote:
>The use case that Alex just posted gave me another way to think about it:
> for item in allitems:
> if fraboozable(item):
> print "first fraboozable item is", item
> print "Sorry, no item is fraboozable"
>If I think of the else statement as being related to the if statement
>inside the loop, then it makes some sense--the else statement is
>executed when the if statement never comes up true. (It's twisted
>logic, I know, but at least it helps me keep track of what "else" does
Maybe it's twisted logic, but it's the only time I've ever used for/else
in real code. It helps if instead of thinking of the else as connected
to the if, you think of the entire for loop as being an extended if
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"It is easier to optimize correct code than to correct optimized code."
More information about the Python-list