[Python-ideas] except expression

Chris Angelico rosuav at gmail.com
Sun Feb 16 14:12:30 CET 2014


On Sun, Feb 16, 2014 at 11:16 PM, Philipp A. <flying-sheep at web.de> wrote:
> i wouldn’t count dicts (and slices), as they are literals of sorts, but
> lambdas are exactly what we’re sidcussing about: a expression, instisting of
> sub-expression, one of which is after a colon.

Braces syntax for dicts is somewhere between an expression and a
literal. Syntactically it's not a literal, as can be shown with dis:

>>> def f():
    return {1:2, 3:4}

>>> dis.dis(f)
  2           0 BUILD_MAP                2
              3 LOAD_CONST               1 (2)
              6 LOAD_CONST               2 (1)
              9 STORE_MAP
             10 LOAD_CONST               3 (4)
             13 LOAD_CONST               4 (3)
             16 STORE_MAP
             17 RETURN_VALUE

But to the human, it's convenient to think of it as a "dict literal"
in the same way that you can have a "string literal" or "float
literal". You can compare something against a "literal", for instance:

>>> f() == {3:4, 1:2}
True

However, it's not a literal, as it can have arbitrary expressions for
both keys and values:

>>> {int("123"):float("234")}
{123: 234.0}

> [ Steven said: ]
> “pass” is a placeholder null statement, it has no relevance to
> try…except. You might just as well sensibly say
>
> stomach_content = eat() except ThinMintError import explode()
> stomach_content = eat() except ThinMintError del explode()
> stomach_content = eat() except ThinMintError class explode()
> stomach_content = eat() except ThinMintError def explode()
> stomach_content = eat() except ThinMintError pass explode()
>
> In all of these cases, I have picked a random keyword and just tossed it
> into the expression. None of them make any sense.
> [ Philipp responded: ]
> hah! now it’s my turn to smugly say “did you even read my post?!” ;)

It's worth noting that the person who originally suggested "pass" here
was me, and I came up with it by the exact method Steven described:
pulled up a list of keywords and picked one that seemed plausible
enough to post... *as a joke*. It wasn't till it was picked up on as a
plausible suggestion that I considered it at all seriously. It was my
preferred form in the first draft; now, my preferred form is with the
colon. The two-keyword notation lent itself well to parallels with
if/else, but since chaining needs to be special anyway, this isn't
going to be a simple operator.

(By the way, Steven, what on earth is a Thin Mint that could cause
your eating to be so exceptional that your stomach explodes? The mind
boggles...)

ChrisA


More information about the Python-ideas mailing list