Short if

Sylvain Thenault sylvain.thenault at nospam.logilab.fr
Wed Jul 7 12:08:05 CEST 2004


On Wed, 07 Jul 2004 09:40:04 +0000, Paul Sweeney wrote:

>> Does Python have a short if like C or PHP:
>>
>>   bool = false
>>   string = 'This is ' + (( bool ) ? 'true' : 'false')
>>
>>
> 
> If you are really convinced that this is a good road to go down (think of
> code readability), you could do
> 
>     bool = False   # check capitalisation! string = 'This is ' +
>     ('false','true')[bool]

you can use logical "and" and "or" to achieve this:

string = 'This is ' + (bool and 'true' or 'false')

-- 
Sylvain Thénault                               LOGILAB, Paris (France).

http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org





More information about the Python-list mailing list