Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
X vs. Y is a common direction that many threads take, in many discussion forums, on many radio stations, in many news broadcasts, and in many political arenas.

This post is another X vs. Y. However, unlike the majority of X vs. Y -- heck, let's even start getting more specific and say Perl vs. Python -- debates, this thread will attempt to measure the differences between these two languages by looking at differences on the coding level. Rather than just "well, it's just easier to do task XYZ in Python/Perl" I intend to do a bit of character-by-character analysis (I think you'll see what I mean) to show you why lately I've taken great interest in Python.

In fact, I'm even yearning to understand why Perl still manages to exceed the popularity of Python.

If you think this is a flame against Perl though, you've drastically missed my point. I'm here to present, from a coding standpoint, why I've come to see Python as an extremely cool language...for me, more useful than Perl I believe, and from there, to be shown -- by you, the viewer -- the contrary to my viewpoint.

Allow me to begin:

  • Variable access -- Perl guarantees that for virtually every time you access a variable, you will have to type at least one more character (and hit Shift!) than Python. And, when use strict (aka, good coding practices) is in effect, at least three extra characters to declare them (spaces are characters too :).
    PerlPython
    @foofoo
    my $foo = 1;foo = 1
  • Perl makes you type {}'s around your blocks and ()'s around your conditionals, which leads to a.) yet more typing, b.) annoying inconsistencies in styles (which doesn't seem like a big deal...until you're the maintenance programmer).
    PerlPython
    if ($foo) {
        print "hello world";
    }
    if foo:
        print "hello, world"
    if ($foo)
    {
        print "hello, world";
    }
    if foo:
        print "hello, world"
    Python's conditional constructs always look the same, which, when you're the maintenance programmer, is a welcome surprise. They also take up fewer lines.

  • Perl guarantees that you'll type at least one extra character at the end of every line. The ";", of course.

  • The Perl Way of function/method parameters (AND setting parameter defaults) takes more keystrokes, is harder to document, and less intuitive (because it's completely different to almost every other language out there, and you actually have to look into the implementation of the function or method to see what the parameters are!):
    PerlPython
    sub foo {
        my $arg1 = shift;
        my @rest_of_args = @_;
        ...do stuff...
    }
    def foo (arg1, rest_of_args):
        ...do stuff...
    sub foo {
        my $arg1 = shift || 0;
        ...do stuff...
    }
    def foo (arg1 = 0):
        ...do stuff...
    Python's syntax above is not only much simpler (i.e. the function definition syntax of having a function name followed by a parameter list in parenthesis, which is common to many languages), but, because it is part of the function definition itself, it's easy to write programs that automatically generate documentation. In combination with Python's "docstrings" (a string literal placed right after the definition of the function, that python's interpreter parses and puts into the variable func_name.__doc__) automatically generating module documentation is easy, without needing to know any freaky perldoc tags. And for lazy programmers, it's nice to save on the keystrokes too.

  • Perl makes you type more to access object properties.
    PerlPython
    $foo->bar();foo.bar()
    In combination with the added typing associated with variable access in Perl, this adds up.

  • Python has multiple implementations. Stackless Python (for smart people, that know how to use it :) and Jython which not only gives you access to virtually any Java class you might want to use, but -- because it's written in pure Java -- allows you to run Python on any Java platform.

  • There are huge chunks of perldoc sections that would make for completely irrelevant sections in Python documentation.
    perlreftut and
    perlref -- In Python, everything already is a reference. There's no need to learn the tricks of using them.
    perlvar -- Python's "special variables" are virtually self-documenting. e.g. func_name.__doc__
    perlboot and
    perltoot and
    perltootC -- Save for perhaps a little blurb thta might equate to a "pythontootC", Python's OO is as obvious as it gets. Want to declare class Foo that inherits from classes Bar and Baz? class Foo(Bar, Baz)...how much easier to you want it? :)
    perlpod -- docstrings are the only really "special" thing you need to know about documenting your Python code. However, there's nothing "special" about them. You write --> "this function does that" instead of --> # this function does that.
    perllol -- because everything in Python already is a reference, lists of lists work the way you think they would. [1, 2, 3] becoming an LoL would simply be (as an example) [1, ['foo', 'bar'], 3]. What other syntax would you have expected? :)
    The point being, fewer rules to learn == fewer rules to keep in your head.
Now...all of the above issues are what I would consider agnostic in religious flame wars. They show specific examples of how Python let's you type fewer characters, and/or do things in a cleaner syntax and/or in ways that lend to automating away tasks like creating HTML or PDF documentation of your code without having to know any special syntax to do it. On the religious side, many people reject Python because it uses indentation levels to group statements. With respect to Perl programmers, there are two serious flaws in this argument:
  1. It amounts to the same argument that non-Perl programmers use in saying "Perl sucks because it looks like line noise". You'll note in my illustration of why I seem to prefer Python these days, I've not used such an argument.

  2. If you wrote a simple parser to remove all the {}'s from Perl code blocks, you'd notice that Perl's code blocks would look EXACTLY the way Python code looks, because indenting code is a requirement of good coding practice.
So, there you have it. An empirical (yes, I've come up with these points from actually having used Python to do everything from script my IRC client, to writing a weather reporting module, to producing an online drugstore for a client) essay on why I think Python seems to win on most counts when measured against Perl. And before you suggest that it seems most points made seem to just say "you type less in Python" a.) carefully reread them because there's definitely more to it than that, and b.) ask yourself...even IF the languages were equal on every other count except that one allowed you to type fewer characters to get your job done, wouldn't you prefer the route of less typing? :)

Please show me a contrary, and equally backed up, viewpoint on this debate. For reasons already cited "it sucks because it uses whitespace to group statements" or "Perl is better because it has default variables" don't fly. That's 100% pure opinion. The points I've shown above are concrete examples of why, even with best coding practices, character for character, and due to language design issues you will save characters in Python, and also shows specifically why it's easy to write tools to autogenerate Python module documentation without knowing any special documentation syntax.

Warning: If you say something about Perl vs. Python in clear ignorance of, and obviously rarely having used, Python, You Will Be Flamed.

Heh.


In reply to Perl vs. Python: Looking at the Code by mothra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (6)
    As of 2019-03-26 17:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How do you Carpe diem?





      Results (125 votes). Check out past polls.

      Notices?