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

John Bokma john at castleamber.com
Tue Mar 30 21:22:42 EDT 2010


Robert Fendt <no.spam at local.local> writes:

> And thus spake John Bokma <john at 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



More information about the Python-list mailing list