Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'operator': 0.04; 'subject:Python': 0.05; 'python': 0.08; 'readable': 0.09; 'def': 0.13; '();': 0.16; 'ed.)': 0.16; 'list1,': 0.16; 'misrepresent': 0.16; 'perl.': 0.16; "would've": 0.16; 'x1,': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'perl': 0.18; 'sfxlen:2': 0.19; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'received:209.85.213.46': 0.23; 'received:mail-yw0-f46.google.com': 0.23; 'cc:2**0': 0.26; 'code': 0.26; 'import': 0.27; "i'm": 0.28; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.29; '3.x': 0.30; 'skip:\xc2 20': 0.30; 'sub': 0.30; 'quite': 0.31; '---': 0.31; "i've": 0.32; 'that,': 0.32; 'there': 0.33; 'wondering': 0.34; 'probably': 0.35; 'but': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'should': 0.38; 'received:209': 0.39; 'quick': 0.61; 'more': 0.61; 'march': 0.61; 'show': 0.66; 'as:': 0.70; 'skip:\xc2 10': 0.74; 'claims': 0.80; 'list2)': 0.84; 'quote,': 0.84; 'to:addr:mail.python.org': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=Hhu6+16CQp6X79arJradwBWXm+hKmCsnt33ExILlVJ4=; b=Az7TVs3TnFDSAGCMgs4QLfSRZdLyl+lIAlukLU5FBqVkxz+Xdg9v0YG3ryAWqmExnY bekAQO3OnfU0DU+lEbD23fRdJ/93mB1u1HbhDrEU3sHzkt20GGUg1xQtHSrgLCyuaX0K zKF716FsikoDGRI3BQ2W5bvz7wrJhTGf+4XmkjrAuuWUYLAYBV140bszioHSnjqBbPkU 8T2Q/XuMuCfNx/pIqfegqrOOgfvVRqURmcQbNJLyGdhmyjG78dyDTgGNfiSPH+g83TnA l61pfLGPyMqf0DRqA8BQZ4fnTsX4/uMaiEjG5a7zPioOSFT0mmbk2h4xV5oScKBc8aXq PIew== MIME-Version: 1.0 In-Reply-To: <4f612b19$0$1379$4fafbaef@reader2.news.tin.it> References: <4f612b19$0$1379$4fafbaef@reader2.news.tin.it> Date: Wed, 14 Mar 2012 23:54:31 +0000 Subject: Re: Python is readable From: Arnaud Delobelle To: Kiuhnm Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331769273 news.xs4all.nl 6950 [2001:888:2000:d::a6]:47883 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21635 On 14 March 2012 23:34, Kiuhnm wrote= : > I've just started to read > =C2=A0The 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 { > =C2=A0 =C2=A0my($arg1, $arg2) =3D @_; > =C2=A0 =C2=A0my(@result) =3D (); > =C2=A0 =C2=A0@list1 =3D @$arg1; > =C2=A0 =C2=A0@list2 =3D @$arg2; > =C2=A0 =C2=A0for($i=3D0; $i < length(@list1); $i++) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0push(@result, $list1[$i] + $list2[$i]); > =C2=A0 =C2=A0} > =C2=A0 =C2=A0return(\@result); > } > > --- Python --- > def pairwise_sum(list1, list2): > =C2=A0 =C2=A0result =3D [] > =C2=A0 =C2=A0for i in range(len(list1)): > =C2=A0 =C2=A0 =C2=A0 =C2=A0result.append(list1[i] + list2[i]) > =C2=A0 =C2=A0return result > --- --- > > It's quite clear that he knows little about Perl. > Here's what I would've written: > > sub pairwise_sum { > =C2=A0 =C2=A0my ($list1, $list2) =3D @_; > =C2=A0 =C2=A0my @result; > =C2=A0 =C2=A0push @result, $list1->[$_] + $list2->[$_] for (0..@$list1-1)= ; > =C2=A0 =C2=A0\@result; > } > > Having said that, the Python code is still more readable, so there's no n= eed > 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: from operator import add def pairwise_sum(list1, list2): return map(add, list1, list2) --=20 Arnaud