[Python-Dev] Adding C ternary select (a?b:c) to Python?
Ka-Ping Yee
ping@lfw.org
Mon, 31 Jan 2000 22:27:02 -0600 (EST)
On Mon, 31 Jan 2000, Tim Peters wrote:
> [Tim]
> >> 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.
>
> [Ka-Ping Yee]
> > Actually, i'm afraid i don't. I initially chose the "then/else"
> > spelling specifically because "if" flags the eye to the beginning
> > of a statement. My line of thinking was, "'then' for expressions,
> > 'if' for statements."
>
> OK, I'm baffled. I probably don't recall your suggestion -- the implication
> is that it didn't use the word "if"? If so, I probably read it and assumed
> you left out the "if" my mistake <wink>.
Yeah, my suggestion was, e.g.
def abs(x):
return x > 0 then x else -x
Might as well summarize the other suggestions so far:
return x > 0 ? x else -x
return x > 0 ? x : -x
return if x > 0: x else -x
Have i missed any?
Oh, yes, and here is the control group.
return x > 0 and x or -x
return (x > 0 and [x] or [-x])[0]
if x > 0:
return x
else:
return -x
> Seriously, "excessively novel" isn't called for here:
> *tons* of languages have used if/then/else for this
> purpose without difficulty.
Yes, you're right about that.
> No keyword has been added to Python since "lambda", and you can be certain
> Guido will never add another (at least not to Python1) -- this is an
> absolute non-starter. Ping, *you* used to know this better than anyone
> <wink>.
Okay, okay. You probably have a better memory about this than i do. :)
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.
Another issue with the last suggestion: how do you explain putting a
colon after the condition but not after the "else"?
-- ?!ng