[Python-Dev] PEP 463: Exception-catching expressions

Ethan Furman ethan at stoneleaf.us
Sat Feb 22 09:06:42 CET 2014

On 02/21/2014 10:57 PM, Stephen J. Turnbull wrote:
> Ethan Furman writes:
>> On 02/21/2014 07:46 PM, Chris Angelico wrote:
>>> but not this:
>>> value = expr except Exception: default except Exception: default
>> This should be the way it works.  Nothing is gained in readability
>> by turning a try with multiple except statements into an
>> expression.
> Examples have been given several times.  In general, if 'expr' is a
> function call, it may well have a couple of different ways to fail
> which imply different default values.
>      interpolable = func(key) except TypeError: "not a string: %s" % key \
>                               except KeyError: "no such key: %s" % key
>      print("Some message that refers to '%s' % interpolable")
> versus
>      try:
>          interpolable = func(key)
>      except TypeError:
>          interpolable = "not a string: %s" % key
>      except KeyError:
>          interpolable = "no such key: %s" % key
>      print("Some message that refers to '%s' % interpolable")
> I think the latter begs to be written as the former.

Okay, that's the best example of that style I've seen so far (sorry, Chris, if something similar was in the PEP and I 
missed it).  I will yield the point that something is gained -- still, I think it is a small something compared to 
converting a nested except statement into an expression, and if only allowing one or the other makes the whole thing 
simpler I vote for the nested excepts to be converted, not the already easily read multiple excepts.


More information about the Python-Dev mailing list