For review: PEP 308 - If-then-else expression

Andrew Dalke adalke at
Sun Feb 9 03:22:59 CET 2003

Carlos Ribeiro:
> Thanks again for pointing out the type of situation where I expect
> expressions to be most useful. I find it amazing that many people don't
> regard long blocks of code as a problem; as for myself, I get lost with
> blocks that don't fit on the screen.

My analysis suggests that this will, on average, make code
about 0.04% smaller.  This was based on analyzing the number of
time ?: is used in a large C++ codebase which was written by
programmers of moderate experience and counting the times it was
used appropriately (for C++), appropriately (if it was Python), and
inappropriately (for either language).  It was used inappropriately
more than it was used appropriately.

Andrew Koenig suggests a higher use rate, but that was based on C code
(so no objects, which mitigate a few of the problems) written by an
expert programmer.  His rate was 1/400 lines, of which I think a few
were debateable and a few more unneeded in Python.  Apply a scaling
factor and assume it's used once per 200 lines of Python code, and
that each use saves 3 lines.  Then the code is, on average, 1.5%

In addition, it was rare to have more than one use of ?: in the same code
block, and in at least one of those cases the code should have been
written with a loop, turning 16 lines into 7 or so.-

So while you may expect it to be useful for those cases, in normal
practice it does not turn out to be the case.

                    dalke at

More information about the Python-list mailing list