for: else: - any practical uses for the else clause?
Klaas
mike.klaas at gmail.com
Fri Sep 29 14:26:10 EDT 2006
metaperl wrote:
> Actually right after posting this I came up with a great usage. I use
> meld3 for my Python based dynamic HTML generation. Whenever I plan to
> loop over a tree section I use a for loop, but if there is no data to
> iterate over, then I simply remove that section from the tree or
> populate it with a "no data" message.
else: does not trigger when there is no data on which to iterate, but
when the loop terminated normally (ie., wasn't break-ed out). It is
meaningless without break.
Python 2.4.3 (#1, Mar 29 2006, 15:37:23)
[GCC 4.0.2 20051125 (Red Hat 4.0.2-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in []:
... print 'nothing'
... else:
... print 'done'
...
done
-Mike
More information about the Python-list
mailing list