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

Nick Coghlan ncoghlan at gmail.com
Fri Feb 21 15:57:37 CET 2014


On 22 February 2014 00:44, Eli Bendersky <eliben at gmail.com> wrote:
> True, but at least you still have to explicitly try...except... which takes
> a toll on the code so isn't taken lightly. Adding except into expressions, I
> fear, will proliferate this usage much more.

The same fears were raised regarding conditional expressions, and
they'll be dealt with the same way: style guides and code review.

That's also why the examples part of the PEP is so important, though:
this is about providing a tool to *improve* readability in the cases
where it applies, without significantly increasing the cognitive
burden of the language.

One example not mentioned in the PEP is that "text.find(substr)" would
become just a shorthand for "text.index(substr) except ValueError:
-1", but the latter has the benefit that you can substitute "None"
instead, which avoids the trap where "-1" is a valid subscript for the
string (but doesn't give you the answer you want).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list