473,498 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

deepcopy alternative?

I have a very complex data structure which is basically a class object
containing (sometimes many) other class objects, function references,
ints, floats, etc. The man for the copy module states pretty clearly
that it will not copy methods or functions. I've looked around for a
while (prob just using the wrong keywords) and haven't found a good
solution. As a workaround I've been using cPickle, loads(dumps(obj))
which is incredibly slow (~8 sec for a 1000 elem list).

I believe the only thing stopping me from doing a deepcopy is the
function references, but I'm not sure. If so is there any way to
transform a string into a function reference(w/o eval or exec)?

Example
from copy import deepcopy
a = ClassObj([ClassObj([3.2, 'str', funcRef]), 4, ClassObj[funcRef])
b = deepcopy(a)
TypeError: function() takes at least 2 arguments (0 given)

All I want is a deepcopy of the list. Any suggestions would be appreciated.

Jack Trades

PS: If the answer to this is in __getstate__() or __setstate__() is
there some reference to how these should be implemented besides the
library reference?
Jan 29 '07 #1
3 5483
I believe the only thing stopping me from doing a deepcopy is the
function references, but I'm not sure. If so is there any way to
transform a string into a function reference(w/o eval or exec)?
what's your python version?
for me deepcopy(lambda:1) does not work in py2.4 but it works in py2.5
(in py2.4 i tried to override __deepcopy__ but it had no effect)

Jan 30 '07 #2
En Mon, 29 Jan 2007 20:17:11 -0300, <"none <"@bag.python.orgescribió:
I have a very complex data structure which is basically a class object
containing (sometimes many) other class objects,
What are "class objects"? instances of a given class, or a class itself?
function references,
ints, floats, etc. The man for the copy module states pretty clearly
that it will not copy methods or functions. I've looked around for a
while (prob just using the wrong keywords) and haven't found a good
solution. As a workaround I've been using cPickle, loads(dumps(obj))
which is incredibly slow (~8 sec for a 1000 elem list).

I believe the only thing stopping me from doing a deepcopy is the
function references, but I'm not sure. If so is there any way to
transform a string into a function reference(w/o eval or exec)?
What do you mean by "function reference"? A module-level function works
fine both with pickle and copy. Methods do not; but it's not common to
take methods out of a class and put them inside a tuple, by example...
>
Example
from copy import deepcopy
a = ClassObj([ClassObj([3.2, 'str', funcRef]), 4, ClassObj[funcRef])
b = deepcopy(a)
TypeError: function() takes at least 2 arguments (0 given)
Please provide a *running* example. I've added some definitions, corrected
some syntax errors, and this full example works for me:

--- cut ---
from copy import deepcopy

class ClassObj(object):
def __init__(self, args):
self.data = args

def funcRef(x):
print x

a = ClassObj([ClassObj([3.2, 'str', funcRef]), 4, ClassObj([funcRef])])
b = deepcopy(a)
print a is b
a.data.append('hello')
print len(a.data), len(b.data)
--- cut ---
PS: If the answer to this is in __getstate__() or __setstate__() is
there some reference to how these should be implemented besides the
library reference?
Until you provide more info on your problem it's hard to tell.
--
Gabriel Genellina

Jan 30 '07 #3
Szabolcs Nagy wrote:
>I believe the only thing stopping me from doing a deepcopy is the
function references, but I'm not sure. If so is there any way to
transform a string into a function reference(w/o eval or exec)?

what's your python version?
for me deepcopy(lambda:1) does not work in py2.4 but it works in py2.5
(in py2.4 i tried to override __deepcopy__ but it had no effect)
Thanks that fixed the problem real quick :)

Jack Trades
Jan 30 '07 #4

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

Similar topics

7
3946
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
0
1327
by: phil | last post by:
I wrote the following to prove to myself that deepcopy would copy an entire dictionary which contains an instance of a class to one key of another dictionary. Note that after copying adict to...
6
3095
by: phil | last post by:
I posted the following yesterday and got no response and did some testing simplifying the circumstances and it appears that deepcopy fails when the object to be copied contains a reference to a...
0
1372
by: Robin Becker | last post by:
I'm using deepcopy in some code which eventually ends up by crash witht he following rather long winded error. I'm not directly using _hashlib.HASH, but I suppose something else along the way could...
0
7126
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,...
0
7210
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...
1
6891
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...
0
7381
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...
0
5465
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,...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.