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

Anders J. Munch andersjm at
Mon Feb 10 20:48:38 CET 2003

"Andrew Koenig" <ark at> wrote:
> Because this proposal is incomplete, I don't know how to evaluate it;
> but I think that if you fill in the details, you will find it
> to be larger than mine.

Here's a complete set with all options included.  Based on the grammer
in the 2.2 docs.

 # Two new nonterminals:
 if_expr_list ::=
              if_expr ( "," if_expr )* [","]

 if_expr ::=
              "if" comparison ":" if_expr "else" ":" if_expr
               | expression

 # Three productions modified by replacing expression_list by if_expr_list:
 parenth_form ::=
              "(" [if_expr_list] ")"

 string_conversion ::=
              "`" if_expr_list "`"

 subscription ::=
              primary "[" if_expr_list "]"

Tally: Three productions added, three modified.  Incidentally this is
exactly the same as your proposal.  Did I miss something?  Note that
not allowing if_expr at top level in slicings and list comprehensions
is deliberate.

> Anders> I think requiring parens like this would be a good idea with all
> Anders> syntaxes, lest we want to see
> Anders>     if A if B if C else D else E:
> This example is obviously illegal on several counts--not the least
> of which is the missing colons--so I'm not worried.

It's written in Guido's notation from the PEP.  Translated into the
notation we are discussing it becomes

  if if if C: B else: D: A else: E:

With required parentheses that would be:

  if (if (if C: B else: D): A else: E):

IMO much easier to read.  Granted a very contrived example.

- Anders

More information about the Python-list mailing list