Is there something similar to ?: operator (C/C++) in Python?
D H
d at e.f
Fri Jun 24 13:54:34 EDT 2005
Riccardo Galli wrote:
> 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
So anywhere someone uses x?y:z you recommend they use "x and y or z"
instead, or else "[y,z][x]", but not:
if x:
val = y
else:
val = z
I still would recommend just using an if statement, even though it is
not easier to type than a ternary expression. It is the most readable
and understandable equivalent in Python. And since his question was
about an easier way to do it in Python, and I don't believe your
alternative are any easier either, I believe the answer is still no.
More information about the Python-list
mailing list