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

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri Apr 11 19:47:02 2003


Hi Scott,


> > My advice is to forget this. Python has its own ways to handle "shared
> > values": classes or nifty default-argument-tricks or ?. It's already
> > hard enough with just this stuff. No need to reimplement perl
> > behaviour or programming style (No, i'm not saying perl's bad, but I'm
> > saying that python is a different language).
>
> 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.
      ^^^^^

I think the operative word here is "trick".  *grin*


There is no trick necessary to do nested data structures in Python, which
I think is the main point of contention here!  Let's dig in and talk more
about this.


In older versions of Perl, the symbolic name trick you showed us,

### (Perl < 5) code
###
$one = 1;
$two = 2;
%dict = ( a => 'one', b => 'two' );
print ${ $dict{a} }, "\n"
###

was necessary to get nested structures working: Perl didn't have proper
references before Perl 5, so it used 'symbolic' references to compensate.
This is a hack because, to get another depth of data structure in there,
we'd need to introduce another set of variables.  And arbitrarily deep
data structures?  Ouch.


In Perl 5, the language introduced the idea of "hard" references to avoid
this soft-reference hack.  In fact, most Perl programmers today would (or
should!) discourage the code above because of its use of "symbolic"
references.  I'm surprised no one at the Perl tutor group mentioned it:
please tell them not to use symbolic references!

Since Perl 5 has real hard references, Perl programmers should prefer the
following code:

### Perl 5 code
$hash = { a => { one => 1 },
          b => { two => 2 } }
###

That is, $dict is a reference to a hashtable that maps to another
reference to a hashtable.  This is a true nested data structure, and
generalizes well to deeply nested structures.



The equivalent code in Python actually doesn't look that different than
this.

###
dict = { 'a' : { 'one' : 1 },
         'b' : { 'two' : 2 }}
###

And this is the way we can create deeply nested data structures.



> I wanted some opinions from the Python list as to whether or not this is
> a drawback or not from a Python programmers perspective and if there are
> any easier solutions than using vars() or eval() that might work better
> for deeply nested structures, or is there a better way to do the data
> structures altogether.

It's a different way altogether: we do not need to use the vars() or
eval() hack in Python or Perl to get nested data structures right.


So our point of confusion was based on the Perl 4 way of doing nested
structure.  You need to tell your friend to review 'perl perlreftut': they
should see some new stuff in there that they haven't seen before.



Please feel free to ask more questions about this: I hope that this clears
up some of the confusion!