Explanation of list reference
dave em
daveandem2000 at gmail.com
Fri Feb 14 13:54:29 EST 2014
On Friday, February 14, 2014 11:26:13 AM UTC-7, Jussi Piitulainen wrote:
> dave em writes:
>
>
>
> > He is asking a question I am having trouble answering which is how a
>
> > variable containing a value differs from a variable containing a
>
> > list or more specifically a list reference.
>
>
>
> My quite serious answer is: not at all. In particular, a list is a
>
> value.
>
>
>
> All those pointers to references to locations are implementation
>
> details. The user of the language needs to understand that an object
>
> keeps its identity when it's passed around: passed as an argument,
>
> returned by a function, stored in whatever location, retrieved from
>
> whatever location.
Jessi,
Thanks for your quick response. I'm still not sure we understand. The code below illustrates the concept we are trying to understand.
Case 1: Example of variable with a specific value from P 170 of IYOCGWP
>>> spam = 42
>>> cheese = spam
>>> spam = 100
>>> spam
100
>>> cheese
42
Case 2: Example of variable with a list reference from p 170
>>> spam = [0, 1, 2, 3, 4, 5]
>>> cheese = spam
>>> cheese[1] = 'Hello!'
>>> spam
[0, 'Hello!', 2, 3, 4, 5]
>>> cheese
[0, 'Hello!', 2, 3, 4, 5]
What I am trying to explain is this, why in case 1 when acting on spam (changing the value from 42 to 100) only affects spam and not cheese. Meanwhile, in case two acting on cheese also affects spam.
Thanks and v/r,
Dave
More information about the Python-list
mailing list