for: else: - any practical uses for the else clause?

Fuzzyman fuzzyman at gmail.com
Tue Sep 26 18:14:34 EDT 2006


Amaury Forgeot d'Arc wrote:
> metaperl.etc at gmail.com a écrit :
> > A very old thread:
> > http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gst&q=for+else&rnum=9#1542d2041257c47e
> >
> > discusses the optional "else:" clause of the for statement.
> >
> > I'm wondering if anyone has ever found a practical use for the else
> > branch?
> >
>
> I use it regularly in contructs like:
>
>      for value in someList:
>          if someCondition(value):
>              print "a matching item was found"
>              break
>      else:
>          print "no matching item"
>          return False
>
>      # ... continue with value

Me too...

Same with while loops.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

> 
> -- 
> Amaury




More information about the Python-list mailing list