Short if
Chris S.
chrisks at NOSPAMudel.edu
Thu Jul 8 15:47:50 EDT 2004
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]
>
> I never liked the C ? operator, always finding code was clearer using the
> full if, and this hack is even worse!
>
It's not about code readability. It's about functional programming.
Lambda expressions don't make very readable code, but Python supports
them anyway. If Python is serious about it's functional aspects, it
requires a ternary operator. Only in the context of functional
programming is this relevant, which makes the whole "it's ugly" argument
moot. Besides, a real ternary, instead of
((condition) and [truecase] or [falsecase])[0]
looks a whole lot better IMO.
More information about the Python-list
mailing list