[Python-Dev] Requesting pronouncement on PEP 463: Exception-catching expressions

Barry Warsaw barry at python.org
Wed Mar 12 21:14:45 CET 2014


On Mar 12, 2014, at 10:40 AM, Ethan Furman wrote:

>Does this mean a better motivation and rationale may cause you to change your
>mind?
>
>My motivation is for simpler, easier to read code: instead of a full-blown
>try/except block or a double-lookup into an indexable object I would much
>rather do:
>
>     self.date = text_to_date(date, 'mdy') except ValueError: batch_date
>
>instead of:
>
>     try:
>         self.date = text_to_date(date, 'mdy')
>     except ValueError:
>         self.date = batch_date

I have to challenge a couple of assertions here.  First, that the exception
expression is easier to read.  I'm not sure it is.  I suspect they will tend
to be expressed on very long lines, much like ternary expressions are today.
PEP 8 then would have you split them across multiple lines, which introduces
parentheses or backslashes to accomplish.

For ternary expressions, this can get ugly and for me sometimes draws me back
to traditional if/else statements.  Other times, ternaries can be worth it,
but either way, it does (again for me) reduce the readability argument.

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.

So the second assertion is that these will be common enough to warrant
changing the language.  Again, I'm not personally convinced.

I echo Guido though: it is a well written PEP, and I commend Chris for
navigating the python-dev/python-ideas gauntlet!  That's an accomplishment
even if the PEP is rejected.

Cheers,
-Barry


More information about the Python-Dev mailing list