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

Steve Holden steve at holdenweb.com
Fri Apr 2 10:21:52 EDT 2010


Steve Howell wrote:
> On Apr 2, 2:04 am, Steven D'Aprano <st... at 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/




More information about the Python-list mailing list