"?:", "a and b or c" or "iif"

Magnus L. Hetland mlh at idt.ntnu.no
Thu May 27 08:21:05 EDT 1999


Thomas Wouters <thomas at xs4all.nl> writes:

> On Tue, May 25, 1999 at 04:35:19PM +0000, Fred L. Drake wrote:
> 
> >   Assuming you're emulating "x ? a : b"....
> >   This isn't quite it.  Using C's ?:, only one of a or b is evaluated
> > when the expression is evaluated.  iif(x, a, b) evaluates both a and
> > b.  Sometimes this is acceptable, but not if either a or b has side
> > effects or is expensive to evaluate.
> 
> [..]
> 
> >   Perhaps Python 2 can fix this shortcoming; I'd often like to use
> > something equivalent to ?:.
> 
> Wait, wait. What's wrong with using 
> 
> x and a or b

Well -- it's not correct :)

x = 1
a = 0
b = 1

x ? a : b returns 0
x and a or b returns 1

--

  Magnus
  Lie
  Hetland        http://arcadia.laiv.org <arcadia at laiv.org>




More information about the Python-list mailing list