#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    66
    Rep Power
    13

    considering moving to python....


    Hiya,

    I've read up a little on python, and it looks like a great scripting language. In the past I've done all my scripts with Perl or Bash, but Python looks like a great alternative. I love the simplicity of the scripts, the great readability, and the fact that it's OO.

    So, my question is this... is Python a viable alternative to Perl? I love the fact that Perl is so extensible (CPAN) and that the community is large, but the language can be pretty cryptic at times, and it's hard for me to remember all of the logistics of the language (I'm more familiar with PHP, Java, and C). I intend to use Python for text processing, and for basic console utilities.

    Anyways... enough rambling, clue me in to why this language is so great, and why it's not!

    Justin
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    I programmed in Perl for several years before I discovered Python, and I have never regretted switching. The functionality provided by many of the libraries that you find in CPAN are either unneccessary in Python, or are included in the standard libraries. There is also the Python Package Index (PyPI) which is a similar repository to CPAN, plus the Vaults of Parnassus.

    IMHO Python is both more readable and writeable than Perl - it is easier to write in Python because you don't have to remember lots of special cases and contexts. I find when I am programming in Python I can concentrate on solving the problem at hand, rather than wrestling with the language.

    There is one area where I would still consider using Perl - writing zero-length scripts with the -e 'command' option, usually in conjuction with -p or -n. e.g.

    perl -p -e "s/perl/python/" *.txt

    to do a search and replace, or

    perl -n -e "print if /fish/ && /chips/" *.txt

    to grep for lines that contain both the words "fish" and "chips". My rule of thumb is that if a script is too long to put on the command line, then write it in Python.

    I recommend that you read the article by Open Source guru Eric Raymond on his experience in switching from perl to python: http://www.linuxjournal.com/article.php?sid=3882

    Dave - The Developers' Coach
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Also coming from a perl background - i'm often surpised how many Python users are - i think theres alot to be said for both languages and there are parts i like from both i.e. the built-in regex in perl. Since i picked up Python a year or so ago i havn't touched perl at all (well maybe once or twice ) and i've never been happier .

    So yes Python is definatly a good alternative to perl; admitadly the community is smaller than that of perls right now but IMO much friendlier and generally more willing to help.

    http://www.python.org/doc/Humor.html

    Put simply... Pythons mix of eligant, clear syntax and power; in the form of its rich standard library and builtin types and functions etc. really do lend themselves intuativly to nearly anything you might want to do with them! And whats more you'll find yourself having more fun too.

    If your ever worried about preformance you can easily write exentions in C/C++ or Java (using Jython). You can also try one of the many different tools which can be used to boost Pythons speed i.e. Psyco. Although personally i've never had any problem in this area!

    Dont take our word for it though - try it yourself, or - go right to the source and ask Yoda what he thinks .


    EXTERIOR: DAGOBAH -- DAY
    With Yoda strapped to his back, Luke climbs up one of the many thick vines that grow in the swamp until he reaches the Dagobah statistics lab. Panting heavily, he continues his exercises -- grepping, installing new packages, logging in as root, and writing replacements for two-year-old shell scripts in Python.
    YODA: Code! Yes. A programmer's strength flows from code maintainability. But beware of Perl. Terse syntax... more than one way to do it... default variables. The dark side of code maintainability are they. Easily they flow, quick to join you when code you write. If once you start down the dark path, forever will it dominate your destiny, consume you it will.
    LUKE: Is Perl better than Python?
    YODA: No... no... no. Quicker, easier, more seductive.
    LUKE: But how will I know why Python is better than Perl?
    YODA: You will know. When your code you try to read six months from now.
    But seriously, give it a go and you wont regret it... or i've never met anyone who has so .

    Have fun,

    Mark.
    Last edited by netytan; May 1st, 2004 at 10:17 AM. Reason: typo
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    66
    Rep Power
    13
    Thanks for the responses! I actually did read the article by Eric Raymond late last night after posting. Excellent article, but he had one unexplained statement that made me curious:

    "For tiny projects (100 lines or fewer) that involve a lot of text pattern matching, I am still more likely to tinker up a Perl-regexp-based solution than to reach for Python."

    Why is this? Are regex faster in Perl, or just easier to code?
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Perl was built to manipulate text and its built-in regex are a powerful asset... not because they are faster than in Python but simply because they are always available (where as in Python the re library must be imported) and are supported as a key part of the language itself, hence perl is better by design for this kind of work; in popular oppinion.

    The reason Eric mentioned the 100 line limit is because more often than not when your perl programs lengh reaches 100+ lines it becomes fairly difficult to manage go figure .

    Hope this clears things up,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo