dave em writes: > 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. > > 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
In case 1, you have only assignments to variables. After spam = 100, the value of spam is another number. The previous number 42 itself is still 42 - number are not mutable, and no attempt was made to change the number. In cheese = spam, cheese is the variable while spam is a variable reference and stands for 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] The first two statements in case 2 are assignments to variables, just like in case 1, but the third statement is different: it doesn't change the value of the variable (the value is still the same object) but it does change the value (replaces one element of the list with another). You don't need to mention the variable cheese here. Note how the value of spam is now different (though still the same object), even though you didn't mention spam at all when you changed it. Python syntax to replace an element of a list looks much like an assignment - many languages do this - but it's not. Behind the scenes it's a method call >>> cheese.__setitem__(1, 'spam') where cheese is a variable reference. You are calling a method of the list that is the value of the variable. > 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. Would it help to say that in case 1 the relevant statement acts on the variable while in case 2 it acts on the value of the variable? This is accurate, I just don't know if it happens to be the thing that helps. One last thing: a variable is not an object. -- https://mail.python.org/mailman/listinfo/python-list