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

Riccardo Galli riccardo_cut1 at cut2_sideralis.net
Fri Jun 24 13:42:34 EDT 2005


On Fri, 24 Jun 2005 09:00:04 -0500, D H wrote:

>> Bo Peng wrote:
>>
>>> I need to pass a bunch of parameters conditionally. In C/C++, I can
>>> do func(cond1?a:b,cond2?c:d,.....)
>>>
>>> Is there an easier way to do this in Python?
>>
>>
> The answer is simply no, just use an if statement instead.

That's not true.
One used form is this:
result = cond and value1 or value2

which is equal to
if cond:
   result=value1
else:
   result=value2


another form is:

result = [value2,value1][cond]


the first form is nice but value1 must _always_ have a true value (so not
None,0,'' and so on), but often you can handle this.

Bye,
Riccardo

-- 
Riccardo Galli
Sideralis Programs
http://www.sideralis.net



More information about the Python-list mailing list