(a==b) ? 'Yes' : 'No'
showell30 at yahoo.com
Fri Apr 2 06:16:18 CEST 2010
On Mar 30, 8:40 am, gentlestone <tibor.b... at hotmail.com> wrote:
> Hi, how can I write the popular C/JAVA syntax in Python?
> Java example:
> return (a==b) ? 'Yes' : 'No'
> ; first idea is:
> return ('No','Yes')[bool(a==b)]
> Is there a more elegant/common python expression for this?
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.
More information about the Python-list