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

Steve Holden steve at holdenweb.com
Tue Mar 30 13:56:04 EDT 2010


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.
> 
Yes, that's deliberately awful syntax. Guido designed it that way to
ensure that people didn't aver-use it, thereby reducing the readability
of Python applications. Speaking purely personally I hardly ever use it,
but don't dislike it.


regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list