Weird exception handling behavior -- late evaluation in except clause

Chris Angelico rosuav at
Mon Dec 3 06:24:50 CET 2012

On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano
<steve+comp.lang.python at> wrote:
> Consider this piece of legal Python code:
> Err = None
> if condition(x) > 100:
>     Err = OneException
> elif another_condition(x):
>     Err = AnotherException
> try:
>     spam(a, b, c)
> except Err:
>     recover()

Legal it may be, but are there times when you actually _need_ this
level of dynamism? It strikes me as a pretty weird way of going about

I agree with the point you're making, but this feels like a contrived
example, and I'm curious as to whether it can be uncontrived.


More information about the Python-list mailing list