[Python-ideas] except expression

Andrew Barnert abarnert at yahoo.com
Thu Feb 20 02:51:28 CET 2014


From: Chris Angelico <rosuav at gmail.com>

Sent: Wednesday, February 19, 2014 5:18 PM
> Subject: Re: [Python-ideas] except expression
> 
> 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


I hated the colon at first, but after seeing it in more simple cases like this one, I'm starting to warm to it. And I definitely like it better than the arrow. For reasons you mostly already elaborated:

> Advantages of the colon include:

> * It's already in use in most programs (how many Python programmers
> even know about the arrow?)

Yes. The colon "looks more like Python" here—maybe only because you don't see function annotations nearly as often as compound statements, lambda expressions, dict displays, etc., but that's still compelling.

Also, I've never really liked -> anywhere, but that may just be because I first used that in ML, and we were using a textbook that used -> but an implementation that used =>, so feel free to discount that too.


> * Parallel with the way lambda uses a colon to separate its arg list
> from a sub-expression

I'm not sure how much this is worth. It loomed big in my head right after trying to write the most pythonic except-expression in Haskell and then translating it to Python. But that's equivalent to the fact that if you wanted to do this with a function instead of syntax you'd have to use a lambda, which is just as true of the if expression or a short-circuiting or or and expression, which don't need colons.

> * Parallel with statement form of "except X:"

I think this is the one that sells it to me.

> * 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
> 


More information about the Python-ideas mailing list