Is there something similar to ?: operator (C/C++) in Python?

Roy Smith roy at panix.com
Sat Jun 18 21:26:48 EDT 2005


Bo Peng <bpeng at rice.edu> wrote:
> I need to pass a bunch of parameters conditionally. In C/C++, I can do
> 
> func(cond1?a:b,cond2?c:d,.....)

Python does not have a ternary operator analogous to C's :?.  There are 
some ugly hacks you can play with the logical operators to emulate :?, but 
in my opinion, the cure is worse than the disease.

> In Python, I am using temporary variables like
> 
> if cond1:
>    para1 = a
> else:
>    para1 = b
> 
> # ....
> # a bunch of such if/else
> 
> func(para1, para2,...)

Yeah, that's how I would do it.  How many of these things do you have?



More information about the Python-list mailing list