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

Andrew Dalke adalke at mindspring.com
Mon Feb 10 00:57:54 EST 2003


Erik Max Francis:
> That's not the point.  The point is whether or not the use of the
> conditional operator in those cases is justified and helps readability.
> You may not think that in the sqrt case it is more readable, but it is
> at least somewhat readable if you know what a conditional operator is.
> The point is _not_ whether or not such a thing can be recasted in some
> other form.

Not the complete point, which I would argue is:

  If the if/else expression is added to Python, will that construct
    - promote use, and if so, by how much?  (Both in the number of uses
         and the amount of improved readability)

    - promote misuse, and if so, by how much?

My analysis of C/C++ code suggests the number of uses in Python
code is on the order of about 1 every 1,000 lines.  Others have come
up with smaller numbers, down to 1 every 200 lines (but I think that
analysis is wrong).  So let's say it's 1 every 400 lines, which I still
assert is more often then it will be used in real Python code.

Now my other analysis suggests a misuse rate roughly equal to
a use rate.  Eg, people will use

  a = x if x < y else y
instead of
  a = min(x, y)

and people will do

  s = chr(48 + i if i < 10 else 55 + i)
instead of
  s = "0123456789ABCDEF"[i]

I have yet to see an analysis which suggests my estimate of misuse
rate is high, but then again there are 330 messages still pending.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list