[Python-ideas] except expression

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Feb 20 04:13:56 CET 2014


On 19/02/14 22:04, David Mertz wrote:
> I don't know if this really amounts to a *strong* objection.  To me, as much
> as I try to like it reading this thread, the colon just continues to feel
> wrong to me.  Yes, of course I know the analogy with lambda, and I can even
> see a certain analogy with dict literals.  However, far more compelling to me
> is making it look more like the ternary expression (which it is really
> basically a special case of.

The only colon-less version I've seen so far that I could
live with would be

    things[i] except None if IndexError

Attempts to use commas instead of keywords are visually
confusing, and abuses of keywords like "except ... return"
and "except ... pass" just look horrible to me.

Yes, it's inconsistent with the way things are ordered
in the try statement, but I don't think that's necessarily
a fatal flaw. It reads like English, so it's fairly obvious
to anyone reading it what the intended meaning is, and if
you find yourself writing

    things[i] except IndexError if None

and you have even half your wits about you, then you'll
notice that something doesn't make sense when you get to
the 'if'. Also if you try to write something like

    things[i] except IndexError else None

you'll find out it's wrong pretty quickly via a
SyntaxError.

One other objection might be that if you want an 'as' clause
it would have to be

    things[i] except f(e) if IndexError as e

which puts the binding of e after its use. But that's not
unprecedented -- comprehensions do this too.

-- 
Greg



More information about the Python-ideas mailing list