For review: PEP 308 - If-then-else expression

Paul Paterson hamonlypaulpaterson at houston.rr.com
Sun Feb 9 19:20:57 EST 2003


"Erik Max Francis" <max at alcyone.com> wrote in message
news:3E46E5B8.9C11AD00 at alcyone.com...
> Paul Paterson wrote:
>
> > Oops, this doesn't work of course since (1,2,3) <- 2 might mean "less
> > than
> > minus 2"
>
> Still, that approach (instead of reversing the elements, reverse the
> position of the operator) seems reasonable.  Maybe change the operators
> to => and <=, respectively.
>

Which allows a (partial) implementation with Python 2.2 - lazy evaluation is
not supported. Which puts me back in the camp of what we really need is a
lazy tuple - a tuple that evaluates its members as required...

class iif_(tuple):
    def __le__(self, other):
        return self[not other]

    def __coerce__(self, other):
        return (other, self)

    def __ge__(self, other):
        return self[other]

def iif(*args):
    return iif_(args)

print "Start"
for i in (0, 1, 2):
    print i, "is", iif("one", "two", "three") >= i

print "5 is", (5>10) >= iif("greater than 10", "less than 10")
print "20 is", (20>10) >= iif("greater than 10", "less than 10")


Yields...

0 is one
1 is two
2 is three
5 is less than 10
20 is greater than 10







More information about the Python-list mailing list