[Python-Dev] Re: Trinary Operators (original) (raw)

Ka-Ping Yee ping@zesty.ca
Thu, 6 Feb 2003 19:38:23 -0600 (CST)


On Thu, 6 Feb 2003, Shane Holloway (IEEE) wrote:

I was thinking that the semantics of "and" & "or" are the replacement for the trinary operator? Since these operations always return the last evaluated subexpression (the same subexpression that short-circuits the evaluation), they can be used as Gerald outlines above.

Unfortunately, this doesn't work if the result is false. For example, here's a common idiom i use in C:

printf("Read %d file%s.", count, count == 1 ? "" : "s");

If you try to translate this to Python using "and"/"or":

print 'Read %d file%s.' % (count, count == 1 and '' or 's')

...it doesn't work, because the empty string is false.

Alas...

-- ?!ng