From: Steve Howell on
On Mar 30, 8:40 am, gentlestone <tibor.b...(a)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.

From: Steven D'Aprano on
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
From: Duncan Booth on
Steven D'Aprano <steve(a)REMOVE-THIS-cybersource.com.au> wrote:

> Yes, I agree, we should be using the previously well known syntax:
>
> condition -> value_if_true, value_if_false
>
> which was introduced by BCPL in 1966.
>
What, not this?

VALOF TEST condition THEN RESULTIS value_if_true ELSE RESULTIS
value_if_false

which was also introduced by BCPL in 1966.

:^)
From: Steve Howell on
On Apr 2, 2:04 am, Steven D'Aprano <st...(a)REMOVE-THIS-
cybersource.com.au> wrote:
> 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:
>

Of course, I understand that the ternary operator has three arguments,
but it only has two possible outcomes.

You asked me to elaborate on the "Tyranny of Three." Let's say you
have three possible outcomes.

In some languages you would write something like this:

mark = (rolls == 1) && (pins == 10) ? 'strike' :
(rolls == 2) && (pins == 10) ? 'spare' :
'normal'

Many people consider the above very ugly, so they write it like so:

if pins == 10:
if rolls == 1:
return 'strike'
else:
return 'spare'
else:
return 'normal'

Then the next programmer comes along and "cleans up":

if pins == 10:
return 'strike' if rolls == 1 else 'spare'
else:
return 'normal'

Then there is this alternative:

if rolls == 2:
return 'spare' if pins == 10 else 'normal'
else:
return 'strike'

And then:

if rolls == 2:
if pins == 10
return 'spare'
else
return 'normal
else:
return 'strike'

Or even this:

return 'strike' if rolls == 1 else ('spare' if pins == 10 else
'normal')

The "Tyranny of Three" refers to a problem where there are an infinite
number of valid solutions, but none of them have any essential beauty,
so they lead to endless nitpicking and code churn.

From: Steve Holden on
Steve Howell wrote:
> On Apr 2, 2:04 am, Steven D'Aprano <st...(a)REMOVE-THIS-
> cybersource.com.au> wrote:
[...]
>> How is the ternary operator "not really ternary, it's binary"? It
>> requires three arguments, not two, which makes it ternary. In Python
>> syntax:
>>
>
> Of course, I understand that the ternary operator has three arguments,
> but it only has two possible outcomes.
>
That doesn't make it a binary operator. Otherwise what's long
multiplication, which has an infinite number of possible outcomes?

> You asked me to elaborate on the "Tyranny of Three." Let's say you
> have three possible outcomes.
>
> In some languages you would write something like this:
>
> mark = (rolls == 1) && (pins == 10) ? 'strike' :
> (rolls == 2) && (pins == 10) ? 'spare' :
> 'normal'
>
> Many people consider the above very ugly, so they write it like so:
>
> if pins == 10:
> if rolls == 1:
> return 'strike'
> else:
> return 'spare'
> else:
> return 'normal'
>
> Then the next programmer comes along and "cleans up":
>
> if pins == 10:
> return 'strike' if rolls == 1 else 'spare'
> else:
> return 'normal'
>
> Then there is this alternative:
>
> if rolls == 2:
> return 'spare' if pins == 10 else 'normal'
> else:
> return 'strike'
>
> And then:
>
> if rolls == 2:
> if pins == 10
> return 'spare'
> else
> return 'normal
> else:
> return 'strike'
>
> Or even this:
>
> return 'strike' if rolls == 1 else ('spare' if pins == 10 else
> 'normal')
>
> The "Tyranny of Three" refers to a problem where there are an infinite
> number of valid solutions, but none of them have any essential beauty,
> so they lead to endless nitpicking and code churn.
>
The Real Programmer (tm) simply chooses one, implements it and moves on.
While philosophical discussions are interesting they don't pay the bills.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/