From: Steve Holden on
Lawrence D'Oliveiro wrote:
> In message <7316f3d2-bcc9-4a1a-8598-
> cdd5d41fd74b(a)k17g2000yqb.googlegroups.com>, Joaquin Abian wrote:
>
>> (a==b) and 'YES' or 'NO'
>>
>> Yes, ugly
>
> 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
--
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/

From: John Bokma on
Robert Fendt <no.spam(a)local.local> writes:

> And thus spake John Bokma <john(a)castleamber.com>
> Tue, 30 Mar 2010 13:19:19 -0600:
>
>> And
>>
>> a == b and 'Yes' or 'No'
>>
>> isn't a Perl-ism?
>
> I never said that this would be better.

It was not my intention to imply you did. But yet I do see books on
Python mention the and or usage, and personally I think the ... if
.... else is preferable.

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

To me the else is adding unneeded noise, I would just write:

if a==b:
return 'Yes'

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 use(d) the if ... else as follows (per Programming in Python 3):

list = [] if list is None else list

in Perl I would've written:

defined $list or $list = [];

Which I prefer over:

$list = [] unless defined $list;

and more over:

$list = [] if not defined $list;

To me the first Perl form reads as a pre-condition: the list must be
defined *or* make it so.

And maybe you're right, the Python one could've been written:

if list is None:
list = []

which looks, now, also more readable to me as well.

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

I guess the latter. Perl has quite some things that in retrospect
could've been done better. On the other hand, Perl developers are very
fanatic about backwards compatibility, which makes it hard to remove
stuff like unless.

On top of that Perl can't be statically analyzed, meaning you can't just
write a perl4to5 or perl510to512 converter.

> And more importantly (and
> more on-topic here), why we have to have an analogon in Python.

My point. You can sufficiently argue what's wrong with if..else in
Python 3 without dragging Perl into it and staying in Python context. I
am both a Perl and (learning) Python programmer, and to be honest I get
very tired of the somewhat weekly

omg Perl suxxxorsss!!11111

Each language has its own suckage. There are several reasons why while I
am learning Python I also keep updating my Perl skills ;-).

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
From: Russ P. on
On Mar 30, 10:08 am, John Nagle <na...(a)animats.com> wrote:
> Chris Rebert wrote:
> > On Tue, Mar 30, 2010 at 8:40 AM, 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?
>
> > 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.
From: Tim Chase on
John Bokma wrote:

> And maybe you're right, the Python one could've been written:
>
> if list is None:
> list = []
>
> which looks, now, also more readable to me as well.

Though there's a slight difference[1], I'd usually use

lst = lst or []

for your particular initialization use case.

-tkc

[1]
Difference being

>>> lst = []
>>> other = lst
>>> if lst is None: # your code
... lst = []
...
>>> other.append(42)
>>> lst, other
([42], [42])

>>> lst = []
>>> other = lst
>>> lst = lst or [] # my proposal
>>> other.append(42)
>>> lst, other
([], [42])

From: Peter Otten on
Pierre Quentel wrote:

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

next(itertools.takewhile(lambda _: a == b, ["yes"]), "no")

You spoke to soon :)

Peter