[Python-ideas] except expression

Paul Moore p.f.moore at gmail.com
Tue Feb 18 22:20:04 CET 2014


On 18 February 2014 20:55, Chris Angelico <rosuav at gmail.com> wrote:
> Alright, results are in.
>
> Script:
> https://github.com/Rosuav/ExceptExpr/blob/master/find_except_expr.py
> Output:
> https://github.com/Rosuav/ExceptExpr/blob/master/candidates.txt
> Annotated examples:
> https://github.com/Rosuav/ExceptExpr/blob/master/examples.py

Great work!

Looking at the annotated examples, my conclusions would be:

1. The "expr except Exception: default" syntax is actually a lot more
readable in context than I expected.
2. But sometimes in more complex cases the similarity with the
statement form hurts readability.
3. The win is noticeable on the one-line assignments
4. You had a few cases where you could have (should have?) translated
to 3-arg getattr or similar. I'm not quite sure what that says ("if
you have a hammer everything looks like a nail"?)
5. The longer examples typically look more confusing to me than the originals.
6. As you noted, nothing much used multiple exceptions or as. I think
this implies they are of marginal benefit at best (but I'm guessing
just as much as you).

Based on this sample, my main worry with the new syntax is that people
will over-use it. Up to line 125 of the examples I'd say about 50%
seem like wins. After that point the only ones I like are
Lib/tarfile.py and Tools/unicode/comparecodecs.py.

Overall, I think this is a reasonable case for the PEP, but it's not
overwhelming. Which I guess about mirrors my gut feeling for how
useful the new construct would be.

Paul


More information about the Python-ideas mailing list