(a==b) ? 'Yes' : 'No'

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Apr 2 11:04:39 CEST 2010


On Thu, 01 Apr 2010 21:16:18 -0700, Steve Howell wrote:

> The ironic thing about the ternary operator is that it is not really
> ternary; it's binary.  Even just making an expression from a binary
> operator inevitably leads to syntax hell.
> 
> There is a principle of programming that I would like to coin, which is
> the "Tyranny of Three."
> 
> It is impossible to code for any expression that has three possible
> values in any kind of elegant way.  It's just impossible.  Try to code
> the bowling game without tearing out your teeth--three conditions:
> strike, spare, or normal.
> 
> The tyranny of three is that 3 is too small for an elegant N-based
> solution and too large for a simple condition.


I'm afraid I don't understand any of that. Can you explain further?

How is the ternary operator "not really ternary, it's binary"? It 
requires three arguments, not two, which makes it ternary. In Python 
syntax:

value1 if flag else value2

or in C:

flag ? value1 : value2

You say that "Even just making an expression from a binary operator 
inevitably leads to syntax hell."

I don't understand what you mean. What is so hellish about any of these?

a + b  # infix
a b +  # postfix, familiar to anyone who has programmed HP calculators
add(a, b)  # prefix function notation
+ a b  # prefix
add a to b  # verbose English-like


Well, perhaps the infix notation counts as "unusual", and the last as 
"too verbose", but hellish?


-- 
Steven



More information about the Python-list mailing list