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

Christian Tanzer tanzer at swing.co.at
Mon Feb 10 07:05:18 CET 2003


Guido van Rossum <guido at python.org> wrote:

> Requests for an if-then-else ("ternary") expression keep coming up
> on comp.lang.python.

+1 on the idea of an if-then-else expression.

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

+0.3 on this syntax.

I'd like an if-then-else expression  because

- it would eradicate once and for all the ugly and error-prone work
  arounds currently used:

      c and x or y

      (c and [x] or [y]) [0]

      list.pop(c and [x] or [y])

      [x, y] [not c]

- it would reduce redundancy in cases like

      if c :
          t = x
      else :
          t = y

- in some cases, it would reduce flow-breaking detail and improve
  chunking

  compare this:

      important statement
      another important statement
      detail
      detail
      detail
      detail
      next important statement

  to this:

      important statement
      another important statement
      another important statement(..., detail, ...)

  Another case:

      def somefunction(a, b, c):
          4 lines to setup parameter u
          3 lines to setup parameter v
          4 lines to setup parameter w
          do real work
          do some more real work

  vs.

      def anotherfunction(a, b, c):
          do real work(if-then-else expression u)
          do some more real work(if-then-else expression v,
                                 if-then-else expression w)

  Many people think that in such cases one should just factor the
  detail into a separate function. In many cases, that would be the
  right solution. In some other cases though, factoring would be
  wrong -- more of a good thing is not necessarily better.

I wouldn't be happy about any odd syntax for an if-then-else
expression. Some wishes:

- should read well

- should not nest

- avoid precedence ambiguities

- evaluate in one direction

- list true case before false case

- support n-way conditionals if possible

My proposal would be to somehow support lazy dictionaries:

- the values of a lazy dictionary are not immediately evaluated when
  the dictionary is built, but when an item is referenced. For this
  support from the Python parser is necessary.

- consider the kludgy use of normal dictionaries for if-then-else
  expressions:

      { True : lambda : sqrt(x), False : lambda : 0} [x>0] ()

  lots of syntactic ballast.

- compare a hypothetical syntax for lazy dictionaries:

      { True :: sqrt(x), False :: 0} [x>0]

  or maybe:

      lambda { True : sqrt(x), False : 0 } [x>0]

  or even

      lazy { True : sqrt(x), False : 0 } [x>0]

  A better syntax, anybody?

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92






More information about the Python-list mailing list