Short if

vincent wehren vincent at visualtrans.de
Wed Jul 7 05:38:27 EDT 2004


Thomas Lindgaard wrote:
> Hello
> 
> Does Python have a short if like C or PHP:
> 
>   bool = false
>   string = 'This is ' + (( bool ) ? 'true' : 'false')
> 
> ?

Nope. PEP308 to introduce a ternary operator like idiom has been 
rejected (see: 
http://www.sourcekeg.co.uk/www.python.org/peps/pep-0308.html),
but you can emulate it:

 >> bool = False
 >> s = "This is "  + ('false', 'true')[bool]
 >> print s
This is false

In this case bool evaluates to 0 and 'false' is at index 0  of the tuple 
('false', 'true').



--
Vincent Wehren





More information about the Python-list mailing list