473,761 Members | 10,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lambda with copy.deepcopy()

Howdy --

I have a class that has an attribute that is a dictionary that contains
an object that has a kword argument that is a lambda. Confused yet?
Simplified example:

import copy

class Foo:
def __init__(self, fn=None):
self.fn = fn

class Bar:
d = {'foobar': Foo(fn=lambda x: x*x)}

def cp(self):
self.xerox = copy.deepcopy(s elf.d)

When I execute:

b = Bar()
b.cp()

Using Python version:

Python 2.3 (#1, Sep 13 2003, 00:49:11)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin

I get:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in cp
File
"/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/copy.py", line 179, in deepcopy
y = copier(x, memo)
File
"/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/copy.py", line 270, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File
"/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/copy.py", line 179, in deepcopy
y = copier(x, memo)
File
"/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/copy.py", line 307, in _deepcopy_inst
state = deepcopy(state, memo)
File
"/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/copy.py", line 179, in deepcopy
y = copier(x, memo)
File
"/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/copy.py", line 270, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File
"/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/copy.py", line 206, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File
"/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/copy.py", line 338, in _reconstruct
y = callable(*args)
File
"/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/copy_reg.py", line 92, in __newobj__
return cls.__new__(cls , *args)
TypeError: function() takes at least 2 arguments (0 given)

I've googled for deepcopy and lambda and found somebody else asking the
same question on a LUG somewhere, but they gave no advice and nobody
else seems to have run into this. Any ideas on what the problem is/how
to get around it? Thanks!

-jag

Aug 30 '05 #1
0 1704

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

Similar topics

2
35777
by: Andreas Kuntzagk | last post by:
Hi, There are three ways to (shallow)copy a list l I'm aware of: >>> l2=list(l) >>> l2=l >>> l2.copy.copy(l) Are there any differences? Are there more (reasonable) ways? I think the first is the most pythonic, second looks more like this other
2
2245
by: Nick Jacobson | last post by:
This question is with regard to the * operator as used for sequence concatenation. There's the well-known gotcha: a = ] b = a*3 b = 4 print b
6
2232
by: Alexander Zatvornitskiy | last post by:
Hello! I have trouble with copy/deepcopy. It seems, I just don't understand something. Please explain where things goes wrong and how to do it the right way. I have one class: class Distribution: __gr_on_transp=dict() __ostatok_m=dict()
2
12350
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
4
1412
by: KraftDiner | last post by:
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
28
7403
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are "good" in such terms, that this core data structure is changed only by atomic operations, so that the data structure is always consistent regarding the application. Only the change-operations on the dicts and lists itself seem to cause problems...
4
3684
by: Emin | last post by:
Dear experts, I got some unexpected behavior in getattr and copy.deepcopy (see transcript below). I'm not sure if this is actually a bug in copy.deepcopy or if I'm doing something too magical with getattr. Comments would be appreciated. Thanks, -Emin
0
1116
bartonc
by: bartonc | last post by:
I was playing around with the Simple Metaclassing thread and found something odd: >>> class aClass: ... classVar1 = 'hello' ... def __init__(self, arg1): ... self.instVar1 = arg1 ... >>> anInstance = aClass('world') >>> import copy >>> bClass = copy.deepcopy(anInstance.__class__) >>> bClass
3
1581
by: yoma | last post by:
python version 2.5 in module copy we all know that copy have two method: copy() and deepcopy(). and the explain is - A shallow copy constructs a new compound object and then (to the extent possible) inserts *the same objects* into it that the original contains. - A deep copy constructs a new compound object and then, recursively, inserts *copies* into it of the objects found in the original.
0
9945
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9765
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8768
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6599
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
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
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2733
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.