Hi all, 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.
Here are three ways I can think of doing it: ---------- # This one looks ugly def nested_first(x): try: return a(x) except: try: return b(x) except: try: return c(x) except: raise CantDoIt # This one looks long-winded def flat_first(x): try: return a(x) except: pass try: return b(x) except: pass try: return c(x) except: raise CantDoIt # 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 ---------- 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 ---------- I guess what I'm looking for is some sort of if: elif: ... elif: else: but for try: except: That's why try: except: except: ... except: seemed natural to me :) And I'd like to find a nice way to do this in a syntactically correct way. Note: I've chosen functions a, b, c, but really I'm looking for a way that is suitable for any chunk of code. -- http://mail.python.org/mailman/listinfo/python-list