(test) ? a:b

Ned Batchelder ned at nedbatchelder.com
Wed Oct 22 07:30:40 EDT 2014


On 10/22/14 5:05 AM, buscacio at gmail.com wrote:
> Em quarta-feira, 22 de outubro de 2014 06h29min55s UTC-2, ast  escreveu:
>> Hello
>>
>>
>>
>> Is there in Python something like:
>>
>>
>>
>> j = (j >= 10) ? 3 : j+1;
>>
>>
>>
>> as in C language ?
>>
>>
>>
>> thx
>
> without not:
> j = [j+1, 3][j>=10]
> with not:
> j = [3, j+1][not (j>=10)]
>

Why on earth would you recommend this outdated hack, when there's a true 
conditional operator?

     j = 3 if j >= 10 else j+1

Of course, many people feel like the conditional operator isn't worth 
the squished-up unreadability, but if someone asks for a conditional 
operator, at least show them one!

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list