Python for Perl programmers?

Nick Craig-Wood nick at craig-wood.com
Tue Sep 21 05:30:01 EDT 2004


Markus Dehmann <markus.cl at gmx.de> wrote:
>  I am using perl for everything, even bigger programs, with objects,
>  uh, modules and stuff. I know pretty much every trick in perl and have
>  a lot of experience.
> 
>  But I'd like to try a cleaner language, where you don't have to type
>  so much crap to create a class etc. So, I wanna give python a try.

I've recently made that journey myself!  For that reason and the
horrible experience of trying to maintain 25,000 lines of perl code.

>  Is there a tutorial that takes all the standard perl things and then
>  explains how to do them in python? That would be perfect. Open a file,
>  take all the words, put them in a hash,

I recently had a revelation to do with Perl hashes.  Whenever I would
have written a hash in Perl, I now create a class in Python.  The
class can then have documentation and methods.  This is a major
difference for me betwen the two languages - its just so easy to
create a class in Python that whenever you are thinking of a data
structure you create a class (rather than a hash) and then immediately
you get a place to put functions which operate on that data etc.

I always found OO in Perl so clunky that I hardly ever bothered with
it, except if I was making a module.  Python really encourages me to
do OO, and all my python programs are modules anyway.  (I need that
encouragement to use OO since I'm of an age that my first computer
language was BASIC and my second assembler ;-)

>  do something with them, print the result in a formatted way, write
>  it to a new file etc. Create a class that downloads newsgroups,
>  etc. Things like that.
> 
>  I don't need long explanations, but just the perl code and the
>  corresponding python code. Maybe that even helps seeing: ah, python is
>  much cleaner, or shorter, or whatever.

I've found that Python without all those { } @ % $ _ to be quite a bit
cleaner looking than Perl, especially for math heavy code.  The
indentation thing really works.  Docstrings are great.  And the
standard library is very good and very comprehensive - you don't need
to download some module from CPAN of dubious quality - its all in the
standard library, properly documented etc.

Downsides of Python?  __init__ __this__ __that__ __etc__ - I found
that a real turnoff initially! Regexpes a little more clunky.  Same
functionality though.  No command line shortcuts -l -p -i etc.  (Not a
bad thing probably!).

Things which trip my up regularly?  I still forget to put () on method
calls which take no parameters.  Luckily pychecker catches this!

>  I mean, if I want to learn French and I already know a lot about
>  languages, it's probably good not to learn all the grammar, but just
>  to take an English book and the French translation, and learn from it.
>  I want to do the same to learn python.

"Programming Python" by Mark Lutz I found to be helpful as it has lots
of examples.

"Python Essential Reference" by David Beazley was useful for its
Python quickstart and library reference.

Write a python program is my advice though!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list