On Fri, 14 Feb 2014 10:54:29 -0800, dave em wrote: > 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.
A list is a container for multiple values, when you do: cheese = spam You're pointing cheese and spam at the same container. Now anything you do to the container (whether by referencing it as cheese or spam) will affect the container. If you want cheese and spam to start out as separate copies of the same list that you can manipulate independently, then you can use: cheese = [ x for x in spam ] eggs = spam[:] ham = list( spam ) >>> spam = [1,2,3,4,5] >>> cheese = [ x for x in spam ] >>> ham = list( spam ) >>> eggs = spam[:] >>> spam [1, 2, 3, 4, 5] >>> cheese [1, 2, 3, 4, 5] >>> ham [1, 2, 3, 4, 5] >>> eggs [1, 2, 3, 4, 5] >>> cheese[3] = "fred" >>> ham[4] = 'ham' >>> eggs[4] ='eggs' >>> spam [1, 2, 3, 4, 5] >>> cheese [1, 2, 3, 'fred', 5] >>> ham [1, 2, 3, 4, 'ham'] >>> eggs [1, 2, 3, 4, 'eggs'] -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list