[Python-Dev] Requesting pronouncement on PEP 463: Exception-catching expressions (original) (raw)

Chris Angelico rosuav at gmail.com
Wed Mar 12 21:49:47 CET 2014


On Thu, Mar 13, 2014 at 7:14 AM, Barry Warsaw <barry at python.org> wrote:

Interestingly enough, where ternaries are most useful are in the same situations where I think exception expressions would be most useful, in the setting of a variable or attribute to one of two different values. In both branches of the conditional, the left hand side is the same target, and what differs is the value being assigned to the LHS.

Looking over exception examples from a large code base, I find it rare (but not nonexistent) that exceptions are used in the same situation. But that could be because I'm so comfortable with .get() and other helpers. Ternary expressions seem more compelling. But there's definitely some overlap, which is perhaps why the colon seems so jarring to me.

When you say "looking over", do you mean with the Mk 1 Human Eyeball, or with the script from my research?

https://github.com/Rosuav/ExceptExpr/blob/master/find_except_expr.py

If you've used the script and have some stats to offer, I'd love to see them. So far, all I have is the stdlib, which isn't necessarily representative.

Bear in mind, there isn't a .get() for every situation. You can use hasattr() in place of AttributeError, which isn't quite the same thing, but most of the OSError-raising APIs don't (as far as I know) offer a way to return a default instead of raising. So even if you do generally use dict.get(), there'll likely be some that the script would catch.

ChrisA



More information about the Python-Dev mailing list