473,563 Members | 2,762 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 5510
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.o rgescribió:
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
3955
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 arguments, got 0". Tested with Python 2.3.4 on OpenBSD and Python 2.4 on Win98; same results. Is this a bug in deepcopy, how I dynamically assign the...
0
1343
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 ndict I delete adict. Then ndict contains a good copy of adict. works great.
6
3102
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 Canvas Object. Perhaps any Tkinter object, didn't get that far. The problem arises because I have a geometry tutorial with a progression of...
0
1379
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 be. Is there some nice way to make copy/deepcopy give more information when this error happens? I know what the top level object is, but presumably...
0
7664
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
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7638
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
7948
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
3642
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
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 we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
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.