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

Russ P. russ.paielli at gmail.com
Tue Mar 30 22:18:50 EDT 2010


On Mar 30, 10:08 am, John Nagle <na... at animats.com> wrote:
> Chris Rebert wrote:
> > On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.b... 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

Baloney. The Python ternary syntax is perfectly fine. The "if" could
have been put in front, as in Scala:

return if a == b "yes" else "no"

but either way is fine and perfectly readable as far as I am concerned.



More information about the Python-list mailing list