When to use exceptions (was Re: reducing if statements...)

Paul Rubin phr-n2001 at nightsong.com
Wed Aug 22 02:36:07 CEST 2001


> This always seemed pretty clumsy to me.  I prefer to use exceptions:
> 
>     class done(Exception): pass
>     try:
>         for i in range(len(mylist)):
>             for j in range(len(mylist[i])):
>                 if some_predicate(mylist[i][j]):
>                     raise done
>     except done:
>         do_stuff_with(mylist[i][j])
>     else:
>         print "didn't find anything interesting..."
> 
> YMMV.  Some people hate the construct.  I think it's pretty Pythonic.

It's very normal do that in Lisp using catch/throw.  Yet more evidence
that Python is just Lisp without parentheses :-)



More information about the Python-list mailing list