Python is readable

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Mar 16 02:53:44 CET 2012


On Thu, 15 Mar 2012 00:34:47 +0100, Kiuhnm wrote:

> I've just started to read
>    The Quick Python Book (2nd ed.)

Is this the one?

http://manning.com/ceder/


> The author claims that Python code is more readable than Perl code and
> provides this example:
> 
> --- Perl ---
> sub pairwise_sum {
>      my($arg1, $arg2) = @_;
>      my(@result) = ();
>      @list1 = @$arg1;
>      @list2 = @$arg2;

I don't understand the reason for $arg1 and $arg2. Is there some reason 
why the code couldn't do this instead?

       my(@list1, @list2) = @_;


>      for($i=0; $i < length(@list1); $i++) {
>          push(@result, $list1[$i] + $list2[$i]);
>      }
>      return(\@result);
> }
> 
> --- Python ---
> def pairwise_sum(list1, list2):
>      result = []
>      for i in range(len(list1)):
>          result.append(list1[i] + list2[i])
>      return result
> --- ---
> 
> It's quite clear that he knows little about Perl.

On the contrary -- it is quite clear that you are missing the point of 
the comparison, which is not to compare the most idiomatic Perl with the 
most idiomatic Python, but to make a direct comparison of syntax for the 
purpose of teaching beginners.

The problem with idiomatic comparisons is that they often don't give you 
a feel for the overall language syntax. Instead they end up comparing 
built-ins. For example, here is how I would write the above pairwise 
addition using idiomatic RPL, the Forth-like programming language used on 
some Hewlett-Packard scientific calculators:

ADD

That's it. One word. Would you conclude from this that RPL is easier to 
read and write than Python? I can tell you that it is not, and I *like* 
stack-based languages that use reverse Polish Notation.


> Here's what I would've written:
> 
> sub pairwise_sum {
>      my ($list1, $list2) = @_;
>      my @result;
>      push @result, $list1->[$_] + $list2->[$_] for (0..@$list1-1);
>      \@result;
> }
> 
> Having said that, the Python code is still more readable, so there's no
> need to misrepresent Perl that way.

Speaking as somebody who doesn't know Perl, I think that your version 
would do a great disservice to Perl. Your version is shorter, but 
conciseness is often in opposition to readability. Without the author's 
version above, and the function name, I would have literally NO IDEA what 
your version does. It is virtually pure line-noise. I know enough Perl to 
guess that @result might be an array, and so guess that push pushes a 
value onto the array, but the rest might as well be written in Martian. 
Or APL.

The author's version above, which you denigrate, is *much* more 
understandable than your more idiomatic Perl, especially since I can 
compare it feature to feature with the Python code.

Far from misrepresenting Perl, he has gone out of his way to show Perl in 
the best possible light. Idiomatic Perl code, written by experts, is even 
more incomprehensible and unreadable to non-Perl hackers than his example.


> Now I'm wondering whether the author will show me "good" or "bad" Python
> code throughout the book. Should I keep reading?

>From what you have show, and the sample chapters on the link above, I am 
impressed. The author is aiming to teach basic concepts and impart 
*understanding* rather than just force-feed the reader idioms which would 
be incomprehensible to them. Vern Cedar (the author) is an actual 
professional teacher, and from the samples I have seen, he knows what he 
is doing.


-- 
Steven



More information about the Python-list mailing list