(test) ? a:b

Chris Angelico rosuav at gmail.com
Wed Oct 22 11:15:55 CEST 2014


On Wed, Oct 22, 2014 at 8:05 PM,  <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)]

There's a distinct semantic difference there, though. Compare these:

/* C */
int x = (y != 0) ? 65536/y : 0;

# Python
x = 65536/y if y else 0

# Python, your way
x = [0, 65536/y][y!=0]

Do you see where the problem is?

Plus, subscripting a literal list is far less readable than the
ternary operator.

Also: Please can you avoid Google Groups, or if you must use it,
please at least clean up the excessive blank lines before you post.
I've left them so you can see what we have to cope with. Thanks!

ChrisA



More information about the Python-list mailing list