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

Bo Peng bpeng at rice.edu
Sat Jun 18 20:36:06 EDT 2005


Hi,

I need to pass a bunch of parameters conditionally. In C/C++, I can do

func(cond1?a:b,cond2?c:d,.....)

In Python, I am using temporary variables like

if cond1:
   para1 = a
else:
   para1 = b

# ....
# a bunch of such if/else

func(para1, para2,...)

Is there an easier way to do this in Python?

BTW, I do not want to use the following since all of a,b,c,d etc will be 
evaluated.

def condValue(cond, val1,val2):
   if cond:
     return val1
   else:
     return val2

func(condValue(cond1,a,b), condValue(cond2,c,d),...)

Thanks.
Bo



More information about the Python-list mailing list