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

G

gentlestone

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?
 
M

Mike Kent

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?

return ('Yes' if a == b else 'No')
 
C

Chris Rebert

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.

Cheers,
Chris
 
D

Daniel Fetchinson

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?

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

And for less clutter you can even leave the parenthesis:

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

Joaquin Abian

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
 
R

Robert Kern

Chris said:
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
 
J

John Nagle

Chris said:
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
 
S

Steven D'Aprano

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'
 
S

Steven D'Aprano

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

Steve Holden

John said:
Chris said:
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
 
R

Robert Kern

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

I would suggest that this is much more common and less awkward English usage:
"If I leave the office early, I'm going to the movies tonight; otherwise, I'll
stay home and nitpick on Usenet."

I don't have a problem with the current syntax, and while its English analogue
is grammatical, I don't think you can rightly call it idiomatic.

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

Robert Fendt

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.

In fact, the syntax just shouts 'do [...] unless' to me. And
that's so strong a Perl-ism I cannot quite express how ugly I
actually find it...

Regards,
Robert
 
J

John Bokma

Robert Fendt said:
In fact, the syntax just shouts 'do [...] unless' to me. And
that's so strong a Perl-ism I cannot quite express how ugly I
actually find it...

And

a == b and 'Yes' or 'No'

isn't a Perl-ism?

Sheesh, this group would be so much nicer without the constant dragging
in of Perl to make a point. On top of that, do { } unless blocks are
not idomatic in Perl. Perl Best Practices even clearly states to *never*
use unless.
 
P

Pierre Quentel

Robert Fendt said:
In fact, the syntax just shouts 'do [...] unless' to me. And
that's so strong a Perl-ism I cannot quite express how ugly I
actually find it...

And

 a == b and 'Yes' or 'No'

isn't a Perl-ism?

Sheesh, this group would be so much nicer without the constant dragging
in of Perl to make a point. On top of that, do {  } unless blocks are
not idomatic in Perl. Perl Best Practices even clearly states to *never*
use unless.

I'm surprised nobody proposed a solution with itertools ;-)

- Pierre
 
R

Robert Fendt

And

a == b and 'Yes' or 'No'

isn't a Perl-ism?

I never said that this would be better. I don't even get the
point of what the advantage is supposed to be of inverting the
order of the return statement and the conditional check what
should actually _be_ returned. What's wrong with just writing

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

apart from it being a few more line breaks and an additional
return statement? The inverted form is not more readable per
se (in fact, quite the opposite), and I would even suggest to
minimise its use even in languages like C++ and Java. The Python
syntax is even worse since it not only inverts the order of
return statement and conditional check, but it also puts the
conditional between the two results.

I find such a convoluted construct especially ugly in a language
which I previously regarded as having a rather striking beauty
of syntactical simplicity. The construct is superfluous,
illogical, unelegant, and thus very un-pythonesque, IMHO. But of
course that's just my $0.02.
Sheesh, this group would be so much nicer without the constant dragging
in of Perl to make a point. On top of that, do { } unless blocks are
not idomatic in Perl. Perl Best Practices even clearly states to *never*
use unless.

Sorry, but you have just underlined my point, in fact. If it's
discouraged by experts, then of course the question must be
valid why such a feature even exists (okay, apart from 'it
seemed like a good idea at the time'). And more importantly (and
more on-topic here), why we have to have an analogon in Python.

Regards,
Robert
 
S

Steve Holden

Robert said:
I never said that this would be better. I don't even get the
point of what the advantage is supposed to be of inverting the
order of the return statement and the conditional check what
should actually _be_ returned. What's wrong with just writing

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

apart from it being a few more line breaks and an additional
return statement? The inverted form is not more readable per
se (in fact, quite the opposite), and I would even suggest to
minimise its use even in languages like C++ and Java. The Python
syntax is even worse since it not only inverts the order of
return statement and conditional check, but it also puts the
conditional between the two results.

I find such a convoluted construct especially ugly in a language
which I previously regarded as having a rather striking beauty
of syntactical simplicity. The construct is superfluous,
illogical, unelegant, and thus very un-pythonesque, IMHO. But of
course that's just my $0.02.


Sorry, but you have just underlined my point, in fact. If it's
discouraged by experts, then of course the question must be
valid why such a feature even exists (okay, apart from 'it
seemed like a good idea at the time'). And more importantly (and
more on-topic here), why we have to have an analogon in Python.

It exists because people nagged Guido mercilessly until, against his
better judgment, he capitulated.

regards
Steve
 
M

MRAB

Robert said:
I never said that this would be better. I don't even get the
point of what the advantage is supposed to be of inverting the
order of the return statement and the conditional check what
should actually _be_ returned. What's wrong with just writing

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

apart from it being a few more line breaks and an additional
return statement? The inverted form is not more readable per
se (in fact, quite the opposite), and I would even suggest to
minimise its use even in languages like C++ and Java. The Python
syntax is even worse since it not only inverts the order of
return statement and conditional check, but it also puts the
conditional between the two results.

I find such a convoluted construct especially ugly in a language
which I previously regarded as having a rather striking beauty
of syntactical simplicity. The construct is superfluous,
illogical, unelegant, and thus very un-pythonesque, IMHO. But of
course that's just my $0.02.
I think you mean that it's very _un-Pythonic_ (perhaps because it's very
very Pythonesque)! :)
 
L

Lawrence D'Oliveiro

In message <7316f3d2-bcc9-4a1a-8598-
(a==b) and 'YES' or 'NO'

Yes, ugly

Why would you say that’s ugly?

By the way, you don’t need the parentheses.
 
S

Steve Holden

Lawrence said:
In message <7316f3d2-bcc9-4a1a-8598-


Why would you say that’s ugly?

By the way, you don’t need the parentheses.

But at the same time, if you don't *absolutely know* you don't need the
parentheses then the parentheses are a very good idea, so I applaud the
example as Pythonic while agreeing that it's ugly. A conditional
expression seems more natural.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top