something like "if" that returns a value?

Alex Martelli aleax at aleax.it
Mon Nov 11 04:56:57 EST 2002


Paul Rudin wrote:

> 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?

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52310
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52282

The printed (O'Reilly) version of the Python Cookbook has a
better-organized merge of these two recipes -- you can read
it online with O'Reilly's "Safari" service (which is for-pay,
but I think you can get a sample subcription for a few days
for free -- not sure about the latter part, though).


Alex




More information about the Python-list mailing list