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

Hsuan-Yeh Chang hsuanyeh at yahoo.com
Sat Jun 18 21:08:55 EDT 2005


Bo Peng wrote:

> 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

take a look at how to use "lambda" in python.




More information about the Python-list mailing list