[Python-ideas] except expression

Paul Moore p.f.moore at gmail.com
Mon Feb 17 10:56:21 CET 2014


On 17 February 2014 08:40, Steven D'Aprano <steve at pearwood.info> wrote:
> I sympathise, but you already have to deal with dicts, slices, and
> lambda, and with the exception (pun intended) of lambda, they're likely
> to be much more common than except-expression.

I should have expanded :-) (See? I also have to deal with smilies!)

With dicts slices and lambdas, the colon is essentially not so
important for the *human* reader, although it is needed for the
parser. If dicts were simply {key, value, key, value, ...} they would
still make sense to the human reader, just by pairing things up and by
the fact that anyone writing something complicate would lay it out
readably.

When discussing new syntax, it becomes very easy to focus on the
corner cases and how to make them unambiguous and/or clearer than the
alternatives. In my view, that's not the point - what matters is
whether the syntax cleanly expresses the simpler cases that are the
ones motivating the change in the first place. I'm more than happy to
relegate the corner cases to a status of "well, it works, but you
should be using the statement form when it's this complex anyway".

My point with the example I quoted (reproduced here for reference)

>> >     some_io() except (FileNotFoundError: (1, 2, 3),
>> >                       (ValueError, TypeError): 'spam')

is precisely that - the subtle change in meaning based on a relatively
hard to spot colon is *not* something that matters when debating the
relative merits of syntax proposals, because no-one should be writing
code like this in the first place. The parallel with dicts is
misleading, as you can't write my_dict = {1,2:3,4:5,6,7:8} - first of
all, you have to parenthesize the relevant tuples, and secondly nobody
sane would write it like that, they'd use layout to make the intent
clear to the human reader).

Anyway, I don't want to beat up on one example. I'm just trying to
make the points

1. Let's worry about making the basic case as readable as possible,
rather than the complex cases
2. Punctuation for disambiguation can be a problem - I'd prefer it if
the keywords stood by themselves in this case
3. Needing extra parentheses to disambiguate (or even requiring them -
consider yield expressions or generator expressions) is not a bad
thing, nor is needing to make complex cases multi-line - human
readability is more important than using the minimum syntax that the
grammar requires.

Paul


More information about the Python-ideas mailing list