The ternaery operator
Tobiah
toby at tobiah.org
Thu Mar 16 10:55:05 EDT 2017
On 03/16/2017 01:12 AM, Gregory Ewing wrote:
> Stefan Ram wrote:
>
>> a if c else b
>>
>> Guido has invented something totally new. Why?
>
> It's arguably closer to the way you would say such a
> thing in English.
>
> Consider the following sentences:
>
> "I wear my red shirt if it's Tuesday, else my green one."
>
> "I wear if it's Tuesday my red shirt, else my green one."
>
> Which one sounds more natural?
>
To be fair, the proper comparison would be:
If it's Tuesday, I wear my red shirt, else my green one.
I think it would be nice to have a way of getting the 'true'
value as the return with an optional value if false. The desire
comes about when the thing I'm comparing is an element of a collection:
drugs['choice'] if drugs['choice'] else 'pot'
Then I'm tempted to do:
chosen = drugs['choice']
chosen if chosen else 'pot'
I sometimes feel like doing:
drugs['choice'] else 'pot'
Tobiah
More information about the Python-list
mailing list