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

Glenn Linderman v+python at g.nevcal.com
Sat Feb 22 02:10:50 CET 2014


On 2/21/2014 3:29 PM, Greg Ewing wrote:
> Antoine Pitrou wrote:
>
>>>    lst = [1, 2]
>>>    value = lst[2] except IndexError: "No value"
>>
>> the gain in concision is counterbalanced by a loss in
>> readability,
>
> This version might be more readable:
>
>    value = lst[2] except "No value" if IndexError
>
> since it puts the normal and exceptional values next
> to each other, and relegates the exception type (which
> is of much less interest) to a trailing aside.


Ternary if teaches us that the normal and alternate values should be on 
either end, so the present proposal corresponds to that; ternary if also 
teaches us that nesting affects the right value, in the absence of 
parentheses, this proposal seems to correspond, although requiring 
parentheses or not is presently a topic of discussion in a different 
subthread.

On the other hand, "if" and "except" are opposites in another way: "if" 
is followed by an expression that, if true, means the left value is 
used, and if false, means the right value is used. "Except" is followed 
by an exception that, if thrown, means the right value is used, and if 
not thrown, means the left value is used... but the word "except" is 
opposite in meaning to "if", so this actually fits nicely with English 
definitions.


Here's a challenge: There has been a big thread about None versus (SQL) 
Null. Show how an except: expression can help the DB API more easily 
convert from using None to using a new Null singleton, and you'll have a 
winner :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140221/cecce662/attachment.html>


More information about the Python-Dev mailing list