472,950 Members | 2,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 software developers and data experts.

Copy an Object (Again?)

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.

Jan 6 '06 #1
4 1358
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

Jan 6 '06 #2
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.

Jan 6 '06 #3
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

Jan 6 '06 #4
KraftDiner <bo*******@yahoo.com> wrote:
...
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
Jan 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.