[Python-ideas] except expression
Chris Angelico
rosuav at gmail.com
Thu Feb 20 02:18:07 CET 2014
On Thu, Feb 20, 2014 at 11:15 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> result = 1/x except ZeroDivisionError -> NaN
>
> For the record, I could just as easily live with the colon instead of the
> arrow.
>
Time to open up this branch of the discussion... colon or arrow?
For the purposes of this debate, I'm comparing these two notations,
and nothing else:
result = 1/x except ZeroDivisionError -> NaN
result = 1/x except ZeroDivisionError: NaN
Advantages of the colon include:
* It's already in use in most programs (how many Python programmers
even know about the arrow?)
* Parallel with the way lambda uses a colon to separate its arg list
from a sub-expression
* Parallel with statement form of "except X:"
* Compactness (one character and a space to its right, vs two and
spaces both sides) - a good thing, as this notation tends to want to
be on one line
Advantages of the arrow include ->
* Splits nicely at the arrow: "1/x except ZeroDivisionError\n\t-> NaN"
- doing this with a colon would look like introducing a suite, which
would be extremely confusing
* Unique syntax - can't be confused with starting a suite or initializing a dict
* Comparable amount of visual separation between the three parts. Like
with "if/else", where there's a whole word separating each part, the
arrow gives a decent separator between the exception and the default.
The colon binds the two closely together.
* Distinguishes itself from the statement "except X:", which
introduces more statements.
Both proposals fit into my preferred layout order (meaning the
evaluation order is strictly left to right). Neither adds new keywords
to the language. Both put the word "except" immediately ahead of the
exception list. Either would be plausible.
ChrisA
More information about the Python-ideas
mailing list