Can I use a conditional in a variable declaration?

Ben Cartwright bencvt at gmail.com
Sun Mar 19 05:25:59 CET 2006


volcs0 at gmail.com wrote:
> I've done this in Scheme, but I'm not sure I can in Python.
>
> I want the equivalent of this:
>
> if a == "yes":
>    answer = "go ahead"
> else:
>    answer = "stop"
>
> in this more compact form:
>
>
> a = (if a == "yes": "go ahead": "stop")
>
>
> is there such a form in Python? I tried playing around with lambda
> expressions, but I couldn't quite get it to work right.

There will be, in Python 2.5 (final release scheduled for August 2006):

>>> answer = "go ahead" if a=="yes" else "stop"

See:
http://mail.python.org/pipermail/python-dev/2005-September/056846.html
http://www.python.org/doc/peps/pep-0308/

--Ben




More information about the Python-list mailing list