[Python-Dev] Adding C ternary select (a?b:c) to Python?

Tim Peters tim_one@email.msn.com
Mon, 31 Jan 2000 22:03:59 -0500


[Guido]
> ...
> The solution can be the same as what Algol used: 'if' outside
> parentheses is a statement, and inside parentheses is an expression.
> It's a bit of a grammar rearrangement, but totally unambiguous.

If I didn't know better <wink>, I'd say there's an actual consensus here:
it seems we would all agree to "(if cond then true else false)" spelling.
Not to be overlooked is Christian's enhancement, allowing "elif" too (beats
all heck out of guessing how ?: nests!).

Eric, are you also in agreement?

> However, the added keyword means it won't be in 1.6.

I had already channeled that for the group's benefit <wink>.  For 1.6,
without absurd overloading of some other keyword (like "if cond def true
..."), that seems to leave

    (if cond: true else false)

How much is that hated?  I hate it myself, not least because I'd have to
change IDLE's parser to guarantee it couldn't get confused by the colon
here:

   big = (
       if x >= y:
         x else y)

Given the genesis of these things, Barry would probably have to commit
similar hackery on pymode.

That shouldn't drive it, but it's worth a thought.  The best argument
against any enhancement of this nature was Jim Fulton's argument for it:  it
makes lambda more attractive <wink>.

BTW, I'm not entirely convinced "then" would have to be a keyword to make
"if x then y else z" work:  couldn't the grammar use WORD and verify it's
specifically "then" later?  I'm not impressed by "but that would allow 'then
= (if then then else then)'!" arguments (yes, it would; no, nobody would
*do* that except in an hysterical c.l.py posting <0.1 wink>).