i'm guessing this is the "post-it effect". aa = range(0,10) print aa [0,1,2,3,4,5,6,7,8,9] # what you've done is to use the range function to # create the list of 0 to 9, then you associated the # name aa to the list. a popular teaching analogy # is that of putting a post-it that says aa on the list. bb = aa # now you've created another post-it that has bb # written on it and put that post-it on the list. bb[5] = 0 # you've told the interpreter "say, that list that has # the post-it saying bb on it? well, dig into that list # and change the value of the sixth member to be # 0". the interpreter does it, not caring that there's # also a post-it on the list that says aa. print aa [0,1,2,3,4,0,6,7,8,9] print "hey! what happened to my aa list?!!!" # in python, assignment is not a copy operation # that makes a new thing-um. assignment creates # a reference to an object, much like pasting a # post-it onto the object. you can have lots of # post-its tacked onto an object, it's still the same # object (a rose by any other name...). cc = bb dd = cc ee = aa # now the one and only list is referenced by five # different identifiers, aa, bb, cc, dd, and ee. dd[4] = "john jacob jingleheimer schmidt" print ee [0,1,2,3,"john jacob jingleheimer schmidt",0,6,7,8,9]
On Feb 27, 2008, at 5:13 PM, Keith Suda-Cederquist wrote: > Hi, > > I'm using iPython and I've run into an occasional problem that I don't > understand. Here is what I'm seeing: > > >>aa=range(0,10) > >>bb=aa > >>print aa > [0,1,2,3,4,5,6,7,8,9] > >>print bb > [0,1,2,3,4,5,6,7,8,9] > >> # okay, everything allright at this point > >>bb[5]=0 #change bb > >>print aa > [0,1,2,3,4,0,6,7,8,9] #aa has changed!!! > >>print bb > [0,1,2,3,4,0,6,7,8,9] > > So the problem is that when I change bb, aa also changes even though I > don't want it to. Is this supposed to happen? If it is can someone > explain to me why this is a good thing? and finally, can someone give > me some advice on how to avoid or work-around this problem. > > Thanks, > Keith > > Looking for last minute shopping deals? Find them fast with Yahoo! > Search._______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor