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

2014年2月24日 16:29:23 -0800

Greg Ewing suggested:
>> This version might be more readable:
>>
>> value = lst[2] except "No value" if IndexError
Ethan Furman asked:
> It does read nicely, and is fine for the single, non-nested, case
> (which is probably the vast majority), but how would
> it handle nested exceptions?
With parentheses.
Sometimes, the parentheses will make a complex expression ugly.
Sometimes, a complex expression should really be factored into pieces anyway.
Hopefully, these times are highly correlated.
The above syntax does lend itself somewhat naturally
to multiple *short* except clauses:
 value = (lst[2]
 except "No value" if IndexError
 except "Bad Input" if TypeError)
and nested exception expressions are at least possible, but deservedly ugly:
 value = (lvl1[name]
 except (lvl2[name]
 except (compute_new_answer(name)
 except None if AppValueError)
 if KeyError)
 if KeyError)
 
This also makes me wonder whether the cost of a subscope 
(for exception capture) could be limited to when an
exception actually occurs, and whether that might lower
the cost enough to make the it a good tradeoff.
 def myfunc1(a, b, e):
 assert "main scope e value" == e
 
 e = "main scope e value"
 value = (myfunc1(val1, val2, e)
 except e.reason if AppError as e)
 assert "main scope e value" == e
 
-jJ
-- 
If there are still threading problems with my replies, please 
email me with details, so that I can try to resolve them. -jJ
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to