PEP 308 - ternary operator

Paul Rubin http
Tue Feb 11 16:29:20 EST 2003


"Raymond Hettinger" <vze4rx4y at verizon.net> writes:

> "Erik Max Francis" <max at alcyone.com> wrote in message
> news:3E48B45F.A7E2881A at alcyone.com...
> > Jim Jinkins wrote:
> >
> > > My preference for an immediate-if operator is the traditional '?' from
> > > C:
> > >
> > >     cond ? expr1 : expr2
> > ...
> > > If this is not acceptable my second choice is a functional form:
> > >     iif(cond, expr1, expr2)
> > >
> > > This is not a function, it is a built-in macro.
> >
> > Both of these forms have been explicitly rejected by the BDFL, although
> > for different reasons; check the current state of the PEP.
> 
> Actually, he's wavering on both.  However, the second form would be
> implemented with a keyword instead of a macro, but the effect would
> be the same.  Also, "iif" looks like a typo to him, so it would have
> to be "cond", "test", or some such.

I'm glad if ?: is again under consideration.  It's not necessarily the
best choice, but I felt that the stated reasons for rejecting it were
insufficient.  

A macro like iif (or cond or whatever) is interesting partly because
of the far-reaching implications of having such a thing in Python.
Let's take for granted that it really does have macro-like behavior,
i.e. that the compiler inlines the same conditional bytecode as you'd
get by using ?: or whatever.  (I.e. it's not a function call that
performs more function calls to lazy-evaluate expressions that have
been wrapped in lambdas).  Would there then be a way to add other such
macros?  




More information about the Python-list mailing list