Re: Python is readable

Discussion in 'Python' started by Steven D'Aprano, Mar 16, 2012.

  1. 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 + list2)
    > 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
    Steven D'Aprano, Mar 16, 2012
    #1
    1. Advertising

  2. On 16/03/2012 01:53, Steven D'Aprano wrote:
    > 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 + list2)
    >> 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.
    >
    >


    Well put Sir. And (seriously) when making your comments you show the
    killer instincts of a great bowler in an Ashes Test Match, now could
    there be anything more important in life or showing greater esteem than
    that?

    --
    Cheers.

    Mark Lawrence.
    Mark Lawrence, Mar 16, 2012
    #2
    1. Advertising

  3. On Fri, 16 Mar 2012 13:55:06 +0100, Kiuhnm wrote:


    >> 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) = @_;

    >
    > @_ contains references to arrays. You can't pass two arrays to a
    > function.



    Why ever not? That seems like basic functionality to me. I can't imagine
    any modern language that lacks such a simple feature. Even Pascal allows
    you to pass arrays as arguments to functions.

    Is there some design principle that I'm missing that explains why Perl
    lacks this feature?


    --
    Steven
    Steven D'Aprano, Mar 16, 2012
    #3
  4. > >> 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) = @_;

    > >
    > > @_ contains references to arrays. You can't pass two arrays to a
    > > function.

    >
    >
    > Why ever not? That seems like basic functionality to me. I can't imagine
    > any modern language that lacks such a simple feature.Even Pascal allows
    > you to pass arrays as arguments to functions.
    >
    > Is there some design principle that I'm missing that explains why Perl
    > lacks this feature?



    My understanding isthat it assigns each scalar argument until it finds a list
    to assign and then it assigns everything remaining to the list.

    my @arr = ( 'test', 'blah', '1234', 'boop', 'foo', 'bar' );
    my @arr2 = ( 'adsf', 'qwerty' );
    print "@arr\n";
    my @arr3 = (@arr, @arr2);
    print "arr3:mad:arr3\n";
    my ($arg1, $arg2, @arg3) = @arr3;
    print "arg3:mad:arg3\n";

    bash-3.2$ perl temp.pl
    testblah1234boopfoobar
    arr3:test blah 1234 boop foo bar adsf qwerty
    arg3:1234 boop foo bar adsf qwerty


    I assume this is because it combines both elements of the list into one
    giant list and then if you try and assign two lists it does not know
    where to split it. Now if you pass a reference to the two arrays instead
    of the values it should work as expected, but now you are dealing with pointers /
    references.


    bash-3.2$ cat temp.pl
    my @arr = ( 'test', 'blah', '1234', 'boop', 'foo', 'bar' );
    my @arr2 = ( 'adsf', 'qwerty' );
    print "@arr\n";
    my @arr3 = (\@arr, \@arr2);
    print "arr3:mad:arr3\n";
    my ($arg1, $arg2, @arg3) = @arr3;
    print "arg1:mad:$arg1\narg2:mad:$arg2\narg3:mad:arg3\n";

    bash-3.2$ perl temp.pl
    testblah 1234 boop foo bar
    arr3:ARRAY(0xb2f0f90) ARRAY(0xb2f1020)
    arg1:test blah 1234 boop foo bar
    arg2:adsf qwerty
    arg3:





    Ramit


    Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
    712 Main Street | Houston, TX 77002
    work phone: 713 - 216 - 5423

    --
    This email is confidential and subject to important disclaimers and
    conditions including on offers for the purchase or sale of
    securities, accuracy and completeness of information, viruses,
    confidentiality, legal privilege, and legal entity disclaimers,
    available at http://www.jpmorgan.com/pages/disclosures/email.
    Prasad, Ramit, Mar 16, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Troll
    Replies:
    4
    Views:
    373
    Irene Mettias
    Sep 23, 2003
  2. Chris Angelico

    Re: Python is readable

    Chris Angelico, Mar 15, 2012, in forum: Python
    Replies:
    143
    Views:
    1,464
    Chris Angelico
    Apr 2, 2012
  3. Chris Angelico
    Replies:
    15
    Views:
    270
    Dave Angel
    Mar 24, 2012
  4. Chris Angelico
    Replies:
    4
    Views:
    261
    Chris Angelico
    Apr 3, 2012
  5. Nathan Rice
    Replies:
    30
    Views:
    723
    Steve Howell
    Apr 4, 2012
Loading...

Share This Page