On 2020-08-06 22:52, Chris Angelico wrote:
On Fri, Aug 7, 2020 at 11:01 AM Jonathan Grant
<jonathanallengr...@gmail.com> wrote:

How can we start to revive this PEP? And I completely agree, making the syntax 
`... except ... with ...` is much better than `eor`.


Have a read of the PEP's rejection notice at the top. To revive the
PEP, the objections to it need to be solved.

It seems that the rationale that was used in the PEP was fairly narrowly focused on the comparison with things like dict.get() and the idea of EAFP. A somewhat broader justification might be something along these lines:

In practice, a sizable number of try/except statements do nothing except evaluate a single expression and assign it to a variable in the try block, with a single except block that instead assigns a different value to the same variable. This requires four lines of code even if the expression and exception-set involved are quite simple.

This common pattern is Python's current way of expressing the idea of "I want this, or, if that doesn't work, then this other thing", where "doesn't work" means "raises a certain exception". This is parallel to the if-else ternary operator, but the condition "raises an exception" cannot currently be described with an expression, so if-else cannot be used to handle this case.

Many well-designed functions raise exceptions to signal well-defined error conditions for which the calling code has an easily-expressed alternative value (i.e., "what to use instead of the return value of the function, if it raises like this"). In other words, both the exceptional condition and the desired result when it occurs may be expressed clearly and briefly --- except that it can't, because the try/except structure needed to shim in the alternative is itself so cumbersome. Similarly, in many such situations, the variable assigned inside the try/except is only used once immediately after, and the whole if-this-except-this logic could have been inlined into the following code, if not for the fact that try/except is a statement.

This PEP proposes a clean and simple way to handle this common situation. Just as the if-else ternary operator provides a handy way to encode "X unless condition A, in which case Y", the except operator provides a handy way to encode "X unless EXCEPTIONAL condition A, in which case Y".

--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail."
   --author unknown
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XD35QJMO62KBK4I6P7VVA74NWOF7XKY5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to