(test) ? a:b

BartC bc at freeuk.com
Wed Oct 22 14:08:40 EDT 2014



"Rustom Mody" <rustompmody at gmail.com> wrote in message 
news:7d2ea3c1-504e-4f5c-8338-501b1483df38 at googlegroups.com...
> On Wednesday, October 22, 2014 5:01:08 PM UTC+5:30, Ned Batchelder wrote:
>> On 10/22/14 5:05 AM, buscacio 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?
>
>
> To learn a bit about the interchangeability of control and data 
> structures?
>
> [Just playing devil's advocate]

But it doesn't do the same thing.

Comparing:

 x = cond ? f() : g();       # C version

with

 x = [f(), g()] [cond]

the latter evaluates both f() and g() instead of just one. Apart from being 
inefficient, it can have unintended side-effects.

-- 
Bartc

 




More information about the Python-list mailing list