KraftDiner wrote: > I'm having trouble getting a copy of and object... (a deep copy) > > I'm writing a method that creates a mirror image of an object (on > screen) > In order to do this i need to get a copy of the object and then modify > some > of its attributes. > > I tried: > objs = myListOfObjects > for obj in objs: > if obj.flag: > newObject = copy.deepcopy(obj) > newObject.mirror() > myListOfObjects.append(newObject) > > That doesn't seem to work.. the new object seems to disapear from > existance. > I'm wondering if its a bug in my application or if this is my shallow > understanding of the language. > > TIA > B. > Another remark, are you sure that your "for obj in objs" doesn't get you into an infinite loop? If myListOfObjects is a normal list, your assignment
objs = myListofObjects doesn't make a copy of the list: >>> MyListOfNumbers = [1,2,3,4] >>> numbs = MyListOfNumbers >>> MyListOfNumbers.append(5) >>> numbs [1, 2, 3, 4, 5] If anything, that makes your alorithm hard to read, because you are iterating over a list while appending to it. -- http://mail.python.org/mailman/listinfo/python-list