From: Joaquin Abian on
On Mar 30, 5:40 pm, gentlestone <tibor.b...(a)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?

(a==b) and 'YES' or 'NO'

Yes, ugly

Joaquin
From: Robert Kern on
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(a)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

From: Steven D'Aprano on
On Tue, 30 Mar 2010 08:40:56 -0700, gentlestone 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)]

You don't need the call to bool.

('No','Yes')[a==b]


> Is there a more elegant/common python expression for this?

The above is pretty elegant to my eyes, but you can also do:

return 'Yes' if a==b else 'No'



--
Steven
From: Steven D'Aprano on
On Tue, 30 Mar 2010 10:08:31 -0700, John Nagle wrote:

>> 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.

I used to think so to, but now I like it. It matches common English
syntax like:

"I'm going to the movies tonight, if I leave the office early, otherwise
I'll stay home and nitpick on Usenet."



--
Steven
From: Steve Holden on
John Nagle wrote:
> Chris Rebert wrote:
>> On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.beck(a)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/