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

news.eunet.no Hans-Christian.Holm at nrk.no
Tue May 25 11:40:18 EDT 1999


I see that the FAQ suggest "a and b or c" or similar for the equivalent of
the C ?: construct, but the solutions are not good, and rather ugly for
being Python code. I guess ?: is tricky to implement because of the way
Python uses :, but what about a builtin "iif"? This function exists in other
languages, and is easy to implement:

def iif(x, a, b):
  if x:
    return a
  else:
    return b

I use it quite often, as it does make the code a lot cleaner when the
functionality is needed many times in a program. But I'd like to see it as a
built-in, rather than having to type it in all over the place or put it in
some module I have to remember to distribute!

Hans-Christian Holm
NRK Interaktiv







More information about the Python-list mailing list