"?:", "a and b or c" or "iif"
Berthold Höllmann
bhoel at server.python.net
Thu May 27 10:37:44 EDT 1999
"Magnus L. Hetland" wrote:
>
> "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.
>
But sometimes you want to write short functions, and then something like
>>> from Numeric import *
>>> from Gnuplot import plot
>>> a = arange(0,101,1, Float) /100*pi*3
>>> b = map(lambda x: (sin(x)>=0 and [sin(x)] or [0])[0],a)
>>> plot(transpose(array([a,b])))
or something like
>>> print (x-y != 0 and [z/x-y] or [1000])[0]
could be handy, even if not clear on the first look.
Cheers
Berthold
--
bhoel at starship.python.net
It is unlawful to use this email address for unsolicited ads
(USC Title 47 Sec.227). I will assess a US$500 charge for
reviewing and deleting each unsolicited ad.
More information about the Python-list
mailing list