Jeff McAninch wrote:
> I very often want something like a try-except conditional expression similar
> to the if-else conditional. 

I think it may be done currently with the help of next function:

    def guard(func, *args):
        try:
            return func()
        except Exception, e:
            for exc_type, exc_func in args:
                if isinstance(e, exc_type):
                    return exc_func()
                raise

Example usage:

    a, b, c = 10, 20, 0

    result = a + b/c  # raise ZeroDivisionError

    result = a + guard(lambda: b/c, (TypeError, lambda: 10),
                                    (ZeroDivisionError, lambda: b/2))
    
May be not very concise, but it works...

                                    
-- 
Best regards,
 Alexander                  mailto:alexander.kozlov...@gmail.com

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to