On Sat, Apr 18, 2015 at 10:03 PM, Bill Allen <walle...@gmail.com> wrote: > > On Apr 18, 2015 4:11 PM, "boB Stepp" <robertvst...@gmail.com> wrote: >> >> On Sat, Apr 18, 2015 at 3:28 PM, Bill Allen <walle...@gmail.com> wrote: >> > On Apr 18, 2015 7:50 AM, "Peter Otten" <__pete...@web.de> wrote: >> > >> >> Bill Allen wrote:
[...] >> >> You can test your newfound knowledge by predicting the output of the >> >> following script: >> >> >> >> >> >> a = [1, ["x", "y"], 3] >> >> b = a[:] >> >> >> >> a[1][1] = "hello!" >> >> >> >> print(a) # [1, ['x', 'hello!'], 3] >> >> print(b) # what will that print? >> >> >> >> Think twice before you answer. What is copied, what is referenced? >> >> > print(b) will print the original copy of a which b now references which >> > is >> > [1, ["x", "y"], 3] >> >> Uh, oh! You should have checked your work in the interpreter before >> replying! Peter is being very tricky!! (At least for me...) Look again >> at that list inside of a list and... [...] > Ok, just tried it out. In this example b=a and b=a[:] seem to yield the > same results even after the change to a, which I do not understand. Should > not b be a copy of a and not reflect the change? Like you, I am on the path to learning Python, so I may or may not get all the technical details correct, but here goes (I'm certain that if I take any misstepps --pun intended!--that the ever-helpful crew of professionals will set us both straight): So far the emphasis on your original question has been on the differences between 'references to objects' and the actual 'objects'. I think that for the purpose of your question you can think about 'objects' as some sort of data stored someplace, though this is not technically correct. When an item of data is stored, it is more efficient to store it once and then from that point on use identifiers (Which we are used to thinking of in most instances as 'variables'.) to point to the storage location of that particular item of data. So when you originally said something like: my_list = ['a', 'b', 'c'] The ['a', 'b', 'c'] is the item of data stored and my_list is the identifier identifying *where* this particular item of data is stored. If you then do things like say: some_other_identifier = my_list then you just created a new identifier which gives the same location information to exactly the same piece of data. However, fortunately (Or, unfortunately, depending on your point of view.) you picked a type of data -- a list -- that is *mutable*. Like mutations in genetics, this just means that this item of data is capable of being changed in place, i.e., where it is actually stored in memory. So if Peter had said instead something like (Using the above statements.): some_other_identifier[1] = 'Hello!' I think you understand now that the originally *identified* list would now be ['a', 'Hello!', 'c'] . But Peter's actual example had a list inside of a list and BOTH of these are objects (In our discussion, items of data.) and BOTH of these have this property of being *mutable*. So he stuck an object inside of another object, so to speak. And this inner object has identifiers associated with it, too! Before Peter changed one of these changeable objects, he had: a = [1, ["x", "y"], 3] b = a[:] Now BOTH a[1] and b[1] now identify the location of the inner list object, ["x", "y"] . Apparently, Python, in its ever efficient memory management fashion, when it creates the new object/piece of data a[:], it sees no need to duplicate the inner list object, ["x", "y"], but instead creates another identifier/pointer/reference to this object's location. But since this inner list object is mutable, when you change "y" to "hello!" in b, you also change it in a because both a[1][1] and b[1][1] reference/point to the exact same storage location where this element of the inner list is actually stored. I hope this is helpful, and, if there are any misstepps, that when they are revealed both of our understandings will be enhanced! boB Stepp _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor