Hello Arnaud,

> Imagine I have three functions a(x), b(x), c(x) that each return
> something or raise an exception.  Imagine I want to define a function
> that returns a(x) if possible, otherwise b(x), otherwise c(x),
> otherwise raise CantDoIt.
Exceptions are for error handling, not flow control.

> Here are three ways I can think of doing it:
> ...

> # This one only works because a,b,c are functions
> # Moreover it seems like an abuse of a loop construct to me
> def rolled_first(x):
>     for f in a, b, c:
>         try:
>             return f(x)
>         except:
>             continue
>     raise CantDoIt
My vote is for that one.

> I don't feel happy with any of these.  Is there a more satisfying way
> of doing this in Python? What I would like is something like:
>
> ----------
> # This one isn't correct but looks the clearest to me
> def wished_first(x):
>     try:
>         return a(x)
>     except:
>         return b(x)
>     except:
>         return c(x)
>     except:
>         raise CantDoIt
Again, exception are for error handling, not for flow control.

As a side note, try to avoid "catch:", always catch explicit
exceptions.

HTH,
Miki <[EMAIL PROTECTED]>
http://pythonwise.blogspot.com

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to