Arnaud Delobelle arnodel at gmail.com
Thu Mar 15 00:54:31 CET 2012

```On 14 March 2012 23:34, Kiuhnm <kiuhnm03.4t.yahoo.it at mail.python.org> wrote:
> I've just started to read
>  The Quick Python Book (2nd ed.)
> 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;
>    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.
> 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.
> Now I'm wondering whether the author will show me "good" or "bad" Python
> code throughout the book. Should I keep reading?

I don't know this book and there may be a pedagogical reason for the
implementation you quote, but pairwise_sum is probably better
implemented in Python 3.X as:

def pairwise_sum(list1, list2):
return [x1 + x2 for x1, x2 in zip(list1, list2)]

Or in Python 2.X:

from itertools import izip

def pairwise_sum(list1, list2):
return [x1 + x2 for x1, x2 in izip(list1, list2)]

Or even: