Python is readable
Kiuhnm
kiuhnm03.4t.yahoo.it
Wed Mar 14 19:34:47 EDT 2012
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?
Kiuhnm
More information about the Python-list
mailing list