Copy an Object (Again?)

K

KraftDiner

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.
 
E

Ernst Noch

KraftDiner said:
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.
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

KraftDiner said:
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
.... lst=[1, 2, 3]
....
>>> a=A()
>>> b=copy.deepcopy(a)
>>> a
>>> 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
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

I was not very clear about it
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

even when x=X() and y=copyItSomehowFrom(x)
what is supposed to be copied?
the whole file? but where?

in your case .. what is supposed to happen with the mirrored image?
should it be mirrored inplace
if I recall properly in "import PIL.Image as image" if you image.open
an image and want to mirror it you can create a real copy
and then "save as " it into a file

my 2 cents
 
A

Alex Martelli

KraftDiner said:
objs = myListOfObjects
for obj in objs:
if obj.flag:
newObject = copy.deepcopy(obj)
newObject.mirror()
myListOfObjects.append(newObject)

Never modify the very list you're looping on. I doubt this is the root
of your problem, but, at any rate, loop on a COPY of the list you're
modifying -- e.g. change the first statement to

objs = list(myListOfObjects)


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top