Is there something similar to ?: operator (C/C++) in Python?
d at e.f
Fri Jun 24 19:54:34 CEST 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
>>>>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:
val = y
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