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

Christoph Becker-Freyseng christoph at mmc-startup.com
Sat Feb 8 17:05:56 EST 2003


Hello,

  >
  >     The proposed syntax is as follows:
  >
  >         <expression1> if <condition> else <expression2>

+666 (I love it, but it might be pure evil for Python) for this PEP.

However there are two points that I don't like about it:
- It's not evaluated from left to right possibly making code harder to read
- It's easier to produce ugly code e.g. nesting such expressions without
parantheses might result in an one-liner nobody can "understand" without
carefully analysing the code

Points why I still like it:
- An ifthenelse-expressions is really useful in some cases making the
code shorter and "more straight-foreward"
- The proposed syntax is very natural. You can just read the
ifthenelse-expression and understand it's meaning
- I think this syntax will not cause a lot of "ugly code". While it's
possible to produce such code IMHO it doesn't provoke it. And it's
always possible to write "ugly code" if someone wants to do this.
Additionaly any syntax for an ifthenelse-expression without parantheses
make the code harder to read if it's nested in other statements


Here's one real-life example where the ternary-operator wuld be very 
useful. In Zope you often find code like this (from OFS/CopySupport.py):

     if callable(attr): return attr()
     return attr

IMHO

     return attr() if callable(attr) else attr

is much nicer and more natural. Note that it's not possible using some 
ifthenelse-method or other ways posted like [a,b][c].

The more I read "real" code examples on this PEP, the more I like a 
ternary-operator implemented in the proposed way. Somehow I get used to 
reading such code in non-strict left-to-right order. Another interesting 
point is that often the more important case is the TRUE case, which is 
the one seen at first.


  >
  >     If we could live with adding a new keyword, we could use:
  >
  >         if <condition> then <expression1> else <expression2>
  >
  >     Apart from the problem of introducing a new keyword for a minor
  >     feature, this also suffers from ambiguity at the start of a
  >     statement; for example:
  >
  >         if verbose then sys.stdout.write("hello\n") else None
  >
  >     could be an syntactically correct expression statement, but starts
  >     with 'if', which makes the parser believe it is the start of an
  >     'if' statement.  To resolve this, the syntax would have to require
  >     parentheses, which makes it uglier.  However, this form has the
  >     advantage of evaluating strictly from left to right (not that that
  >     is a requirement for being Pythonic -- list comprehensions don't).
  >
  >

Evaluating strictly from left to right would be a real feature of this
statement. But I think the ambiguity makes it hard to read.


Another Alternative:

You could also change the if-statement to an "if-expression" (don't hate
me for this) which evaluates as the last expression evaluated of the
"then:"- or "else:"- part or None if there was not an expression.
IMHO this is not very pythonic and not usual in any programming-language
I know.

Or there could be the following syntax:

when <condition> then <expression1> else <expression2>


Regards,
      Christoph Becker-Freyseng








More information about the Python-list mailing list