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. >
I think you should provide more code, eg what attributes does your object have? imagine the situation like this >>> import copy >>> class A: ... lst=[1, 2, 3] ... >>> a=A() >>> b=copy.deepcopy(a) >>> a <__main__.A instance at 0x403e3c8c> >>> a.lst [1, 2, 3] >>> b.lst [1, 2, 3] >>> b.lst.append(4) >>> b.lst [1, 2, 3, 4] >>> a.lst [1, 2, 3, 4] or even if you "could" copy instances class X: def __init__(self, filename = "/path/file") self.file = file(filename, "w+") def modifyByteAt(offset): self.file.tell(offset) self.file.write("X") this is untested pseudocode, it should only give you an idea hth, Daniel ps: question to all what is a general approach to copy class instances? write own method or is there some __magic__ attribute or should one use pickle.dump? Regards, Daniel -- http://mail.python.org/mailman/listinfo/python-list