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

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


"news.eunet.no" <Hans-Christian.Holm at nrk.no> writes:

> 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:

Hm... I think strange words like iif are even worse... If it could be
called "if", I guess I could like it, but... (I don't like elsif
either...)

Personally, (and this all seems pretty personal already ;), I think
that :? is a feature that you need to know on an idiom-basis to be
able to read it easily. This is also true about the short-circuit
logic method of Python (which I happen to like :) I think the most
Pythonesque way of doing things is to be more explicit, and less
function-oriented. Instead of writing

  x = iif(a < b, y, z)

or

  z = a < b ? y : z

or

  z = a < b and y or z

you write

  if a < b:
      z = y
  else:
      z = z

This is perfectly readable and standard.

--

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




More information about the Python-list mailing list