PEP308: Yet another syntax proposal

Andrew Dalke adalke at mindspring.com
Tue Feb 11 21:08:21 CET 2003


Paul Rubin:
> I thus have to ask: if CE's are so rare, then why does the subject
> matter to you?  You won't be required to use them in your own code if
> they're added.  And according to your own statistics, you'll very
> seldom have to deal with them in other people's code either.
>
> So I'm sorry, but it really sounds like you're trying to impose your
> stylistic preferences on other people without any practical
> justification.  The other CE opponents' posts don't give that
> impression nearly as much.

I generated two sets of statistics:
  1) use of if/else statements vs. if/else expression vs. short-circuiting
        if/else expression
  2) likelihood of misuse of the if/else expression, given more appropriate
         Python idioms

The later numbers suggest that whe I do "deal with them in other people's
code" then there is somewhere between a 25 to 50% that it will be be
used in place of more appropriate Python code.  This is based on
observing the misuse of ?: in existing C++ code, as in the following
(translated into Python)

n = obj.count() if obj is not None else 0
  .... skip a few lines
print obj.name

(Were this written as an if/else statement, the needless code would more
likely have been cleaned up, I think, because the decision point stands
out more.)

I also observed places where the ?: idiom in C does not naturally
fit a if/else expression in Python, because Python is a different language
than C, as in the following (assume the precondition that 0 <= i <= 15)

  if i < 10:
    hex_c = chr(48 + i)
  else:
    hex_c = chr(55 + i)

which in an PEP 308 world will likely be written as

  hex_c = chr(48 + i if i < 10 else 55 + i)

I find this to be less readable than the first case, and even less
readable than the more Pythonic (IMHO)

  hex_c = "0123456789ABCDEF"[i]

And I and others pointed out places where an if/else expression could
be used, but shouldn't because the code was less readable.  In that
case, I assigned a somewhat low probability (I think 25%) that the
less readable option would be taken.

Were there low chance of misuse, I would not be too concerned.
However, it is this high likelihood of misuse which causes me to be
against this syntax, and to keep posting these statistics.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list