Conditional expressions (again)

Dale Strickland-Clark dale at riverhall.NOTHANKS.co.uk
Thu Oct 25 13:17:53 EDT 2001


Michael Abbott <michael at rcp.co.uk> wrote:

>Sorry to keep banging on about this one.
>
>Well, I don't really think there's a great deal of argument about the 
>syntax.  There are, as far as I'm aware, two live proposals for syntax:
>
>(1)    	x = if a: b elif c: d else: e
>or
>(2)    	x = if a then b elif c then d else e

You missed a good one:

                x = b if a else  d if c else e
implied                         (             )

However, we already have conditional assignment, which I've used
several times when I've really had to:

        x = (b, c)[a]

Obviously this assumes a is logic 0 or 1 which you can force if
necessary:

        x = (b, c)[a != 0]

You can also use the 'iif' function that appears in a number of
languages:

        def iif(test, true, false):
            if test:
                return true
            else:
                return false
--
Dale Strickland-Clark
Riverhall Systems Ltd



More information about the Python-list mailing list