Defending the ternary operator

Anders Hammarquist iko at cd.chalmers.se
Tue Feb 11 21:03:32 EST 2003


In article <b27mc5$bqc$1 at slb6.atl.mindspring.net>,
Andrew Dalke <adalke at mindspring.com> wrote:

>(A true Python expert is unlikely to misuse if/else
>when a better Python solution exists, but I tried to factor
>in that most of the user base is not an expert.)

One of the nice things about Python when I first discovered
it (back in 1.3 days or thereabout) was that it was extremely
quick to master the language. There wes no obscure syntax that
needed to be grasped. I believe this (together with batteries
included) was responsibe for those initial "Hmm, I can't think
of anything more that I need to code to do this, but this can't
be all, can it?" moments. There was no language to fight.

Of course, I too was looking for a ternary, and found the and/or
hack. However, I believe that being forced to use the and/or
hack made me think more about whether a ternary was warranted or
not. Also, using it forced me to learn the Pythonic meaning of
and and or, and so I do not find constructs such as
"spam = eggs and eggs.count()" at all strange. So, in the end,
I think I now write better and more beautiful code as a result
of being forced to abstract away the ternary. (Though the
occasional and/or hack still appears.)

Python is not (or at least has not been) about enabling the
programmer to write code in whatever way she wants (you want the
other P language for that), but about trying to implant good
practices in the programmer, so that she is more likely to choose
a beautiful construct over an ugly one. The only way that this
can be successful is if there are constraints in what can be
done with the language.

BTW, the by far most common complaint I hear from people to whom
I introduce Python is about the fact that it forces you to use
consistent indentation. Now, they usually get over it once they've
done some serious coding, but this is definitely about forcing
the programmer to use good style.

/Anders



-- 
 -- Of course I'm crazy, but that doesn't mean I'm wrong.
Anders Hammarquist                                  | iko at cd.chalmers.se
Physics student, Chalmers University of Technology, | Hem: +46 31 88 48 50
G|teborg, Sweden.           RADIO: SM6XMM and N2JGL | Mob: +46 707 27 86 87




More information about the Python-list mailing list