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

D H d at e.f
Fri Jun 24 14:42:04 EDT 2005


Steven D'Aprano wrote:
> On Fri, 24 Jun 2005 12:54:34 -0500, D H wrote:
> 
> 
>>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
> 
> 
> That's not what he said at all. Nowhere did Riccardo suggest that one or
> the other method was better than if..else. The *most* he did was to
> suggest a personal opinion that cond and value1 or value2 was "nice".

He said "that's not true", when I suggested that there is not something 
easier than ? ternary expressions in Python, and that the original 
poster should use if statements instead.  So yes, that is what he said 
by refuting my suggestion.  He must have misinterpreted my response as 
"if statements are the only way to do something like ternary 
expression", which is not what I said.  Again, I said there is *nothing* 
that is as easy as to use as ternary expressions in Python - use if 
statements instead.


>>I still would recommend just using an if statement, even though it is
>>not easier to type than a ternary expression.
> 
> 
> That's fine. Recommend it all you like. But it isn't the only way, and it
> isn't even arguably the best or easiest or simplest way.

You have assumed the same mistaken interpretation of my remarks as he 
did.   When did I ever say an if statement is the *only* way?  In the 
very note to which you responded, I even acknowledged two other ways of 
doing this.  If statements are in the best and easiest and simplest way 
to do conditionals, however, which is exactly what I was arguing.


> 
>>It is the most readable
>>and understandable equivalent in Python.
> 
> 
> To you maybe. Actually, to me also. But "easier" doesn't just mean
> "easier to read" -- it also means "easier to write", 

I explicitly stated in the very note you replied to that if statements 
are easier to understand and read, yet not easier to write than ? 
ternary conditionals.  So thanks for duplicating my own point.


> Until then, the question is at least partly a matter of personal taste,
> and as such, there is no justification for such sweeping generalities as
> "the answer is simply no, just use an if statement instead".

Except that is the right answer.  It's too bad diehard Python 
evangelists (to borrow another one of Peter Hansen's flames) don't give 
people straight answers when they are looking for a feature that python 
just doesn't have and likely will never have, such as the ? ternary 
operator.  The answer is no, there is nothing as easy to use as that in 
Python, and I suggested using if statements instead.



More information about the Python-list mailing list