[Tutor] Perl vs. Python's way of handling references.

Magnus Lyckå magnus@thinkware.se
Wed Apr 16 10:40:03 2003

Hi Scott, and welcome to Pythondom. I suppose you will soon
figure out which language of Perl and Python you prefer when
it comes to handling different data structures.

Direct comparisions are always difficult, since different tool
lead to different approaches. Using an approach which is optimal
in one language, might be far from optimal in another.

My subjective opinions, based on personal experience, follows...

At 11 Apr 2003 16:13:36 -0700, Scott Chapman wrote:
>I wasn't trying to show the list anything relating to Perl vs. Python.  I was
>demonstrating how Perl does references and how Python does them.  The Perl
>advocate on the Perl Tutorial list was the fellow I was quoting on the
>original post.  He said that you must use vars() in python to do the trick
>like you do in Perl with it's reference syntax.  He said that this is a
>drawback to Python when you get into deeply nested structures.

This reminds me of a friend who was terribly angry over a Macintosh
computer, since no menu items or manuals showed him how to format
a diskette. It was so easy in DOS. You just type "FORMAT A:", insert
the diskette, press enter and wait. Here, he didn't have a clue. Do
you have to buy preformatted Mac diskettes, or what? Finally, he called
a friend, who just told him to insert the diskette in the computer. A
dialog appeared at once, asking him if he wanted to format the diskette.

He would never have had any trouble with this if he had been a
complete computer newbie (or used to Macs). It was his "DOS
conditioning" that prevented him from trying the obvious. In Python
the obvious usually works. In Perl you need to know an obscure way,
and you probably love that or leave it.

This Perl guy is describing a non-issue in Python. To use Python
effectively, you need to understand that variables are references
to objects, as someone pointed out before. It's also important to
understand the difference between mutable and immutable objects.

But data structures and dereferencing problems is simply a non-issue
in Python. You rarely hear people ask about nesting lists in lists etc
in Python, since it's trivial. It just works as you would expect,
unless you have been "exposed" to something like Perl, and become
"damaged" by that. It's just like my friend who never inserted the
diskette since he assumed that it must be more difficult than that.

The one related issue that does appear in Python is that people put
the same, mutable object in several places, and then get surprised
when changing it in one place changes it everywhere. Exactly the same
thing can happen in Perl, but there it's only one of many, many
troubles on the road to a working data structure.

Nested data structures is a kludge in Perl. Initially, you couldn't
create nested structures at all, so eval and typeglob had to be used
to fake it. The use of variable names as strings comes from this stone
age practice. You shouldn't have to use that.

Today, there is support for nested structures in Perl, but it's
so difficult to deal with that it requires more than 30 pages of
explanation in "Programming Perl" (2nd ed). That chapter (ch 4) is
full of descriptions of common mistakes and how differently you
have to do things in different cases. The text is much more about
how to handle the syntax, than about working with data structures.
This is a clear indication of bad design in my opinion.

I never managed to learn it in Perl. In python I never understood
that it was an issue at all. It simply worked.

Just a simple thing like the following python:

 >>> a = [1, 2, 3]
 >>> b = ('x', a)
 >>> c = [a, b]
 >>> print c[1][1][2]

You don't need to be a rocket scientist to write that. As a
little test, I tried to write the same thing in Perl, but I
gave up after a few minutes. It's just to weird for me, with
->, $, $$, @, () vs [], \ etc. Perl is simply too full of
legacy from old unix tools to be close to consistent. Maybe
I'm terribly stupid, but it's as if my IQ drop 30-40% when I
try to solve things in Perl instead of Python. Still, I was
a Perl programmer before I was a Python programmer.

Sure, you can become a master of Perl with practice, but I rather
spend time wrestling with other problems than pure language syntax.
It's not by chance that there are "obfuscated Perl contests" but
no "obfuscated Python contests". Python programmers don't want their
code to be difficult to understand. That's why they stick to Python.

You should also think twice about constructing deeply nested
data structures. If you type "import this" in a recent Python
interpreter, you will get some Python wisdom for free. Note the
fifth row: "Flat is better than nested." :)

A few quotes:

http://mail.python.org/pipermail/python-list/2002-November/129160.html :

>I talked my other colleagues into using {Python} for our CS 1
>course this fall. ... In the past I would be swamped during office
>hours with students wanting help deciphering C++ compiler errors.
>This semester almost nobody has stopped by for syntax issues.

In my opinion, Perl is more difficult than C++ in syntax.

A couple from http://c2.com/cgi/wiki?PythonDiscussion :

>"Python is an excellent language for learning object orientation.
>(It also happens to be my favorite OO scripting language.) ... All
>objects in Python are implemented as hash tables, unlike in Perl, in
>which you have to choose a representation (or looking at it more
>optimistically, where you are free to choose the optimal representation)."
>Sriram Srinivasan, 
><wiki?AdvancedPerlProgramming.htm>AdvancedPerlProgramming, page 120.

>I found it to be one of the most intuitive languages I've ever
>worked with -- you can often just guess the syntax and it works.
>Its other great strength is its scalability; it makes sense from
>command-line interaction all the way up to large-scale modular programs.

Magnus Lycka, magnus@thinkware.se
Thinkware AB, www.thinkware.se