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

Chris Colbert sccolbert at gmail.com
Tue Mar 30 13:12:04 EDT 2010


On Tue, Mar 30, 2010 at 1:08 PM, John Nagle <nagle at animats.com> 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.
>> John Nagle
>> --
> http://mail.python.org/mailman/listinfo/python-list
>
I wouldn't say that. It reads exactly how one would say it. I prefer this
over the ? semantics. Whenever I see that, my mind goes "Does a equal b? If
so, return this, otherwise return that". "Return this if a equals b,
otherwise return that" is much more direct and declaritive IMHO.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100330/fc17d22e/attachment-0001.html>


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /