Python vs. Perl, which is better to learn?

Terry Hancock hancock at anansispaceworks.com
Tue Apr 30 16:43:34 EDT 2002


I was intrigued to find this was a serious discussion and
not a flame war. :-)

Cool -- I just learned a bit of Perl, which is probably
the reverse of most people's experience.  Note that
Perl naivety is to be expected, therefore.  However, I
narcissistically suppose that a reverse perspective
might be useful to somebody...

My frustration with Perl was that "arrays" and "hashes"
were very like "lists" and "dictionaries" except for
one critical point -- they don't allow nesting!  (There
are more subtle differences too -- apparently Perl does
pretty much everything "by value" instead of
"by reference").  I'm not quite experienced enough
with either to fully appreciate the consequences of
this difference, but I know it must have interesting
consequences.  However, I've started calling dictionaries
"hashes" in my head now -- I suppose that's not good,
but it seems more intuitive to me.  "Dictionary" has
too strong a connection with the thing I use to look
up words in (whether paper or electronic).

Also, although Perl does apparently have "objects",
they aren't so easy as in Python.  All of this has
led me to conclude that I will not be "switching" to
Perl anytime soon.

However, Perl does fill its "duct tape" roll pretty
well -- it's possible to compactly write simple
pieces of code to do quick fixes for various string
and file manipulations.  It also does some common
tasks in nicely compact ways (such as the "process
all lines in all files mentioned on the command line,
or failing that, all the lines coming from stdin",
which is basically what "<>" means).  Basically, it
seems to me that if all you ever do is write scripts
of 200 lines or less, Perl may serve you very well.

But if you're like me, and almost every program you
write quickly grows beyond 1000 lines, because you
can't resist adding new bells and whistles, or because
you are actually fusing a lot of your programs into
one or -- more often -- fusing a lot of other people's
programs (i.e. modules / "the batteries") to do a job,
then you'll quickly learn to love the objects, modules,
and superior data structures of Python.  (Actually
Perl may have these structures,  but they aren't obvious
enough to have been taught to me in learning basic Perl --
whereas in Python you learn and use them right away).

Which wording makes it clear that I took a class on Perl.
This was the only way for me to learn it. I learned Python
by myself in about a week, after which I was able to write
useful (if not elegant) programs.  With Perl I tried this
and couldn't get over a fundamental resistence to learning
what seemed like arcane and annoying syntax.  I finally
punted and signed up for a class, which I almost never
do.  I suppose Perl is now the first programming language
I ever learned this way.

Lists in Python finally made me appreciate what Lisp
programmers were on about. As a long time number-crunching/
scientific applications programmer, in the Basic, Fortran,
C, Pascal tradition, I had little understanding of the
utility of things like lists, hashes or dictionaries,
and so forth.  Python really sold me on them, though.

The regular expressions are nice too. There's a lot
of useful tasks that can be solved much more quickly
using them, "line noise" or no.  Since I don't plan
to switch languages for any major project, the main
impact has been to increase my interest in using
Python's "re" module to solve similar problems.

Unfortunately, Python documenters seem to have a bit
of a chip on their shoulder about regular expressions,
whereas the main promoters of it have been long accustomed
to Perl. So most of the RE explanations have a definite
Perl bias.  Python's implementation is a bit different,
so it can be tricky to learn (i.e. using Perl-centric
sources to learn Python's REs).  Thus, while I'm sure
Python's RE implementation is "good" it may not be
"easy" by dent of being contrary to the only "texts"
I can find to learn it from (but see comments below).
 
Christopher Browne <cbbrowne at acm.org> wrote:
> Mind you, I have found that the "More Pythonic Way" of constructing
> complex regexes by building components, like the following, to have
> merit:
> 
>   digit = '[0-9]'
>   date = '\(' + digit + digit + digit + digit + '/' + digit + digit + '/'
>   date = date + digit + digit + '\)'
> 
> By building the regex out of something with named components, I don't
> have the horridness of line noise like:
>    if ($line =~ /.*\s+\d+.\d+\s+\d+.\d+\s+.\d+.\d+/) {
> 
> Ultimately, the answer is that both approaches have their own merits
> and demerits.  The "Perl thing" tends to be shorter, while the "Python
> thing" tends to be more readable.

Thanks for the example -- I think there is some definite
merit to this.  It occurs to me that you might be both more
clear and more compact (and equally pythonic?) to say:

digit = '[0-9]'
date = '\(' + 4*digit + '/' + 2*digit + '/' + 2*digit + '\)'

for the regular expression, so you don't have to count
the digits.  That's starting to get kind of elegant, I
like it.   Somehow it always helps me to be able to
pronounce my code. ;-D  

At the risk of being terribly Python-centric, I think
Python feels like the "second implementation" of a
solution, where you have the benefit of hindsight to
help you design a simple, elegant, and powerful design
from the start, which you got by using the old, less
elegant solution which was simply the accumulation of
patches to your old problems -- which is what Perl
feels like.  Given that Perl was indeed earlier, and
that experience with it has no doubt driven Python's
design, this may be no slight to Perl, in fact.

My Perl teacher described Perl as being "like English",
full of idioms and synonyms and ambiguities resolved
by context -- an organic, naturally evolved solution
to programming problems. If so, Python is probably like
Esperanto -- artificial, clean, regular, and easy to
learn, though perhaps not as creatively expressive in
itself.

One other odd observation -- I've noticed that Python
is very popular among computer graphics/graphic
artists, whereas Perl remains very popular among
musicians. This is a very unscientific, unsupported
observation -- but I noticed it when trying to find
both for a project I was working on.  I have some
weird theories about why this might be so, but they're
probably wrong. :-)

Given that no one speaks computer languages natively,
the huge difference in learning curve between Esperanto,
which was designed for it, and English (I have only
pity for those who are forced to learn it as a foreign
language -- I've tried to do penance by studying Japanese,
but frankly even that is more logical), is surely
an argument in Python's favor.  (In all fairness, I
should admit that I've never actually tried to learn
Esperanto, which is not so popular in America. Also,
it will always be associated with Red Dwarf and
Rimmer for me).

IMHO, of course...

Terry

-- 
------------------------------------------------------
Terry Hancock
hancock at anansispaceworks.com       
Anansi Spaceworks                 
http://www.anansispaceworks.com 
------------------------------------------------------





More information about the Python-list mailing list