[Tutor] Perl vs. Python's way of handling references.
Scott Chapman
scott_list@mischko.com
Fri Apr 11 15:51:01 2003
Here's two code snippets, one in Perl, the other Python:
> $ perl
> $one = 1;
> $two = 2;
> %dict = ( a => 'one', b => 'two' );
> print ${ $dict{a} }, "\n"
> Ctrl-D
> 1
>
> The above in python ... You have to say:
>
> $ python
> Python 2.2.1 (#1, Aug 30 2002, 12:15:30)
> [GCC 3.2 20020822 (Red Hat Linux Rawhide 3.2-4)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
> >>> one = 1
> >>> two = 2
> >>> dict = {'a':'one','b':'two'}
> >>> print vars()[ dict['a'] ]
>
> 1
>
> notice how you have to use the vars function instead of a simple memory
> lookup as you do in perl. Its no big deal for this simple case, but when
> your data structures are deeply nested, it makes a huge difference.
I've been discussing this issue regarding on the Perl Tutor list. I'm new to
Python and fairly new to Perl and I'd like to learn how significant this
issue is "when your data structures are deeply nested" from the perspective
of Python programmers who've had to deal with this issue.
Is there a better way of dealing with this issue that I'm not seeing here
(better than using the vars() construct)?
TIA,
Scott