Proposed PEP for a Conditional Expression

Ken Seehof kseehof at neuralintegrator.com
Thu Sep 13 08:16:31 CEST 2001


> "Terry Reedy" <tjreedy at home.com> wrote in message
> news:OsUn7.4220$5A3.1585645 at news1.rdc2.pa.home.com...
> >
> > "Greg Ewing" <greg at cosc.canterbury.ac.nz> wrote in message
> > news:3BA00239.DECE6412 at cosc.canterbury.ac.nz...
> > > "Terry Reedy" <tjreedy at home.com>:
> > >
> > > > I agree that the above is backwards.
> > > > <cond> if <expr> else <expr>
> > >
> > > No! That sounds way too much like Forth!
> >
> > It is exactly like C, whose ? : equivalent is the impetus for the
> > proposed change.
> > Of course, if you don't like the form of C's conditional expressions,
> > you won't like its translation into Python.
>
> C's syntax doesn't overload an already existing keyword, and is therefore
> arguably clearer.  None of the proposed keyword-overloading syntaxes mesh
> well with list comprehensions.

Seems like
  <expr> if <cond> else <expr>
is analogous to
  '[' <expr> for <var> in <sequence> ']'

Of course, in the former case, the [] is missing because the resulting
expression
is not a list.

Since this is python, :"<expr> if <cond> else <expr>" is the most
appropriate
order, since it unambiguously yields expected results consistent with
English.

If we use the other order, with the right keyword it would be:
  <cond> then <expr> else <expr>
but that smells bad somehow, and it introduces a new keyword. :-(

Also, I would propose that ... else None should be implicit.
   >>> print 'eeek' if pi == 3
   None

But this example is giving me second thoughts about the whole idea.
People could easily be confused and misinterpret the syntax as:
   <statement> if <cond> [ else <statement> ]

- Ken






More information about the Python-list mailing list