[Python-ideas] except expression

Chris Angelico rosuav at gmail.com
Wed Feb 19 03:17:59 CET 2014


On Wed, Feb 19, 2014 at 12:33 PM, Rob Cliffe <rob.cliffe at btinternet.com> wrote:
>
> I agree.  For me, "return" would be the least indigestible - at least it
> suggests getting a value and parking it somewhere.  My support for the colon
> is based on (1) its conciseness (2) its natural meaning in my mind,
> something like "here follows" (3) a somewhat fanatical hankering for
> consistency - there is a colon at the end of an "except" statement.
> (Although actually I would prefer that "if", "while", "try", "except" etc.
> statements didn't require a colon - I still forget to put them in sometimes.
> Not sure where that leaves me.)

Compare all of these:

def f(x): return x + 2
f = lambda x: x + 2
def f(x):
    try: return x+2
    except TypeError: return x
f = lambda x: x+2 except TypeError: x

In each case, the expression form implicitly "gives back" an
expression after a colon, while the statement form implicitly goes and
executes the statement after the colon. Does that make reasonable
sense? "Here we are executing statements. Here, go execute that suite
after that colon." vs "Here we are evaluating an expression. Here, go
evaluate that expression after that colon."

> I confess I don't understand what the issues are that makes the barrier so
> high, and I willingly defer to those who know more about it than I do.  But
> if we admit the possibility, "then" is the best candidate I can think of:
>
>     x = d[y] except KeyError then z

The biggest barrier to adding a keyword is code that uses that word as
an identifier. Imagine if "id" were changed from a built-in function
(which can be shadowed) to a keyword (which can't). Every piece of
databasing code that stores the primary key in "id" would be broken,
as would innumerable other codebases. So the trick is to find
something that makes really REALLY good sense in the context it's
being used in, and really bad sense as an identifier.

Currently, the PEP mentions "then", "use", and "when", and my
preference would be in that order. I suspect "then" is unlikely to be
used as a name in as many places as "when" will be, but I can't be
sure without actually searching the code of the world.

ChrisA


More information about the Python-ideas mailing list