[Python-Dev] Re: Trinary Operators

Gary Herron gherron@islandtraining.com
Thu, 6 Feb 2003 14:46:52 -0800


On Thursday 06 February 2003 02:20 pm, Eric S. Raymond wrote:
> Andrew Koenig <ark@research.att.com>:
> > Eric> Yeeeouch.  And the proposed n-ary form is even worse.
> >
> > On the other hand, we already have the problem with list comprehensions:
> >
> >         [f(i) for i in range(n) if g(i)]
> >
> > Here, every evaluation of f(i) is preceded by an evaluation of g(i).
>
> Uh...but this is one of the reasons I never liked list comprehensions.

Perhaps because I'm a mathematician, I find the list comprehensions
very comfortable.  It corresponds quite well with the mathematical
notation for defining sets: {2*x | for all x in someSet}

In the same way the ternary (and the extended n-ary) form correspond
to the usual mathematical notation for defining a function "in
pieces":

             x for x>=0
 abs(x) = {
            -x for x<0

looks like abs_x = (x if x>=0 else -x)

(Of course I can't typeset it in email, but perhaps one can imagine
the "{" large enough to encompass both lines on the right.)

Gary Herron