Jean-Michel Pichavant <jeanmic...@sequans.com> writes: > What about, > > def myMethod(): > for condition, exitCode in [ > (cond1, 'error1'), > (cond2, 'very bad error'), > ]: > if not condition: > break > else: > do_some_usefull_stuff() # executed only if the we never hit the > break statement. > exitCode = good1 > > return exitCode > > This version uses the 'for ... else' statement.
For..else always has seemed ugly and confusing to me, as does that thing of using the captured loop indexes after the loop finishes. I'd prefer a more functional style (untested): def myMethod(): def success(): do_some_usefull_stuff() return good1 cond_table = [ (cond1, lambda: 'error1'), (cond2, lambda: 'very bad error'), (True, success) ] func = next(f for c,f in cond_table if c) return func() This uses the next() builtin from Python 2.6. You could make it more concise: def myMethod(): cond_table = [ (cond1, lambda: 'error1'), (cond2, lambda: 'very bad error'), (True, lambda: (do_some_usefull_stuff(), good1)[1]) ] return next(f for c,f in cond_table if c)() -- http://mail.python.org/mailman/listinfo/python-list