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

Steve Howell showell30 at yahoo.com
Fri Apr 2 10:31:06 EDT 2010


On Apr 2, 7:21 am, Steve Holden <st... at holdenweb.com> wrote:
> 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?
>

Ok, it's not a binary operator.  I just meant it only really selects
from two possible expressions, so you would think there would be one
good way to express that in code, but of course any variation of the
ternary operator leads to endless debates.

>
>
> > 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.
>

Agreed.  The nitpicking and code churn tends to come up when you have
multiple programmers with different aesthetics, although sometimes
even the solo programmer can get overly fiddly.



More information about the Python-list mailing list