(a==b) ? 'Yes' : 'No'

Robert Kern robert.kern at gmail.com
Tue Mar 30 12:58:39 EDT 2010


On 2010-03-30 12:08 PM, John Nagle wrote:
> Chris Rebert wrote:
>> On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.beck at hotmail.com>
>> wrote:
>>> Hi, how can I write the popular C/JAVA syntax in Python?
>>>
>>> Java example:
>>> return (a==b) ? 'Yes' : 'No'
>>>
>>> My first idea is:
>>> return ('No','Yes')[bool(a==b)]
>>>
>>> Is there a more elegant/common python expression for this?
>>
>> Yes, Python has ternary operator-like syntax:
>> return ('Yes' if a==b else 'No')
>>
>> Note that this requires a recent version of Python.
>
> Who let the dogs in? That's awful syntax.

http://www.python.org/dev/peps/pep-0308/

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list