473,545 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(o bj)
newObject.mirro r()
myListOfObjects .append(newObje ct)

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 1388
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(o bj)
newObject.mirro r()
myListOfObjects .append(newObje ct)

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(of fset):
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(o bj)
newObject.mirro r()
myListOfObjects .append(newObje ct)

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(of fset):
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=copyItSomehow From(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*******@yaho o.com> wrote:
...
objs = myListOfObjects
for obj in objs:
if obj.flag:
newObject = copy.deepcopy(o bj)
newObject.mirro r()
myListOfObjects .append(newObje ct)


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(myListOfOb jects)
Alex
Jan 7 '06 #5

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

Similar topics

42
5723
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC...
7
11607
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 is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an...
42
1815
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC...
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7771
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.