Weird exception handling behavior -- late evaluation in except clause

Chris Angelico rosuav at gmail.com
Mon Dec 3 00:24:50 EST 2012


On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> 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
things.

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.

ChrisA



More information about the Python-list mailing list