Weird exception handling behavior -- late evaluation in except clause

Chris Angelico rosuav at gmail.com
Mon Dec 3 02:38:25 EST 2012


On Mon, Dec 3, 2012 at 6:30 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Yeah, in hindsight it was a pretty crappy example. But this sort of
> dynamism really is useful:
>
> def testRaises(exc, func, *args):
>     try:
>         result = func(*args)
>     except exc:
>         return
>     raise AssertionError("expected exception but didn't get one")
>
>
> def wrap(func, exc, default=None):
>     @functools.wraps(func)
>     def inner(*args):
>         try:
>             return func(*args)
>         except exc:
>             return default
>     return inner

Ah, that makes good sense. The 'except' clause takes a parameter, so
it follows logically that you could pass a parameter to something that
wraps an except clause.

ChrisA



More information about the Python-list mailing list