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

Guido van Rossum guido@CNRI.Reston.VA.US
Tue, 01 Feb 2000 08:37:46 -0500


> 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?

	return if x > 0 then x else -x

In some contexts, parentheses must be used, e.g.

	(if c then a else b)[0] = 1

> > 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. :)

Hm, I was just thinking that 'then' wouldn't be the hardest keyword to
add...  But I should probably stick with Tim's suggestion.

> 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.

But "if" is in good company.

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

Whoever proposed that was terribly confused.

--Guido van Rossum (home page: http://www.python.org/~guido/)