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

Scott Chapman scott_list@mischko.com
Fri Apr 11 16:35:31 2003


On Friday 11 April 2003 13:13, Jay Dorsey wrote:
> Scott Chapman wrote:
> I'm most likely not understanding the question properly (never worked in
> Perl before,
>
> still fairly new to Python), but are you looking for this syntax:
> >>> one = 1
> >>> two = 2
> >>> dict = {'a':one,'b':two}
> >>> print dict['a']
>
> 1
>

This does not work as expected. Chage the value of the variable one and you 
don't change the value of dict['a']:

>>> one = 1
>>> two = 2
>>> my_dict = {'a':one,'b':two}
>>> my_dict['a']
1
>>> one = 3
>>>
>>> my_dict['a']
1
>>> id (one)
135313084
>>> id (1)
135313168
>>> id (my_dict['a'])
135313168

The id()'s show that dict['a'] is not pointing to the variable one but to it's 
value, 1.  This is one of the unique things python does with it's namespaces.

Scott