something like "if" that returns a value?

John Roth johnroth at ameritech.net
Mon Nov 11 17:22:09 EST 2002


"Paul Rudin" <Paul_Rudin at scientia.com> wrote in message
news:u7kfktpi5.fsf at scientia.com...
>
> I'm having a little play with python, just to try it out; and one
> thing that I find I miss from other languages is something like lisp's
> if:
>
> (if x y z)
>
> which returns y if x is true and z otherwise.
>
> I realise that I can define a function that does this for me; but is
> there a succinct way of expressing this in the base language?

You can only define a function that does it if you don't need
short circuit evaluation. That is, any simple function you define
will evaluate all three arguements, whether or not it's needed.

The 'and' 'or' hack is the cannonical way of doing this. It short
circuits properly, and it returns the value of the expression that was
evaluated.

You might be able to do a short circuit functional evaluation
if each of the two expressions is placed in a lambda. However,
this complicates the syntax even further.

John Roth
>
> --
> Am I accompanied by a PARENT or GUARDIAN?





More information about the Python-list mailing list