Defending the ternary operator

Andrew Dalke adalke at
Mon Feb 10 09:21:24 CET 2003

Andrew Koenig:
> The fact that people are suggesting such circumlocutions says to me
> that there is a use for the feature, and the fact that people are
> suggesting incorrect circumlocutions makes my skin crawl.

Hey, you also caught when I used the if/else expression the wrong way ;)

In the universe of all times when if/else might be used, it is relatively
uncommon to actually use it (by my analysis of C/C++ code).  Of those
C/C++ examples, some of the cases had better "Pythonic" examples.
I've given some examples elsewhere.

My estimate is that for every appropriate use of ?: in C/C++ only
1 of those 3 is appropriate for Python because other solutions exist
which *are* natural, and not circumlocutions.  Of those, my estimate
is that about 1/2 of the time someone will choose to use the ?: way
instead of the clearer non-?: solution.

This means that for every two uses of ?: in Python-with-if/else-expressions
which ARE understandable, readable, etc, there will be one to
two cases which ARE NOT.

And that makes my skin crawl.

I prefer no circumlocutions ("use an if/else statement") and no
crawling skin.

                    dalke at

More information about the Python-list mailing list