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

Tim Peters tim_one@email.msn.com
Tue, 1 Feb 2000 03:28:42 -0500


[Ping]
> Yeah, my suggestion was, e.g.
>
>     def abs(x):
>         return x > 0 then x else -x


Yup, I *did* mentally insert an "if" for you.  You're welcome <wink>.

> Might as well summarize the other suggestions so far:
>
>         return x > 0 ? x else -x

Toss it -- *nobody* wants it (if that's one I suggested, I get to retract
it).

>         return x > 0 ? x : -x

Has anyone other than Eric come out explicitly in favor of this spelling (I
know others have come out in favor of a ternary operator, but don't recall
much love for this syntax)?

>         return if x > 0: x else -x

I get to retract that too (heck, I said I hated that one in the same
sentence I tossed it out ...).

> Have i missed any?

Yes, the utterly conventional and perfectly clear one:

    return (if x > 0 then x else -x)

If we don't buy into Guido's keyword argument, it's feasible.

There was also some Perlish atrocity (not that I'm judging ...) like

    return x if x > 0 else -x

> Assuming that "then" will never be made a keyword, i would probably
> go with "x > 0 ? x else -x".  "if" seems to shout "statement" too
> loudly at me, and colons seem too loaded.

Too late:  it was mine & I retracted it <wink>. "if" doesn't really *shout*
stmt unless it's at the start of a line.  Wrap it in parens too and it's
barely a whisper.

> Another issue with the last suggestion: how do you explain putting a
> colon after the condition but not after the "else"?

The truth:  "Because Guido is afraid of new keywords" <wink>.