[ python-Bugs-1277718 ] Lambda and deepcopy

SourceForge.net noreply at sourceforge.net
Thu Sep 1 23:38:13 CEST 2005


Bugs item #1277718, was opened at 2005-08-31 16:10
Message generated for change (Comment added) made by tjreedy
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1277718&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: Python 2.3
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Joshua Ginsberg (jaginsberg)
Assigned to: Nobody/Anonymous (nobody)
Summary: Lambda and deepcopy

Initial Comment:
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(self.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.framework/Versions/2.3/lib/python2.3/copy.py",
line 179, in deepcopy
    y = copier(x, memo)
  File
"/System/Library/Frameworks/Python.framework/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.framework/Versions/2.3/lib/python2.3/copy.py",
line 179, in deepcopy
    y = copier(x, memo)
  File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/copy.py",
line 307, in _deepcopy_inst
    state = deepcopy(state, memo)
  File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/copy.py",
line 179, in deepcopy
    y = copier(x, memo)
  File
"/System/Library/Frameworks/Python.framework/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.framework/Versions/2.3/lib/python2.3/copy.py",
line 206, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/copy.py",
line 338, in _reconstruct
    y = callable(*args)
  File
"/System/Library/Frameworks/Python.framework/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



----------------------------------------------------------------------

>Comment By: Terry J. Reedy (tjreedy)
Date: 2005-09-01 17:38

Message:
Logged In: YES 
user_id=593130

Python does not have lambdas.  It has functions.  Among other 
ways, they can be produced by lambda expressions.  The *only* 
residue of lambda origin is the otherwise invalid 
.func_name '<lambda>'.

General bug-seeking advice:
1. If you can, upgrade to at least 2.3.5, the last 2.3 series 
release, with its several bug fixes.
2. If at all possible, test code with the lastest release.  Each 
release has numerous bug fixes.  (But not needed here.)
3. Reduce code to the minimum needed to get the apparent 
buggy behavior.  You code is mostly noise for this purpose.  I 
believe copy.deepcopy(lambda x: x) would be sufficient to get an 
exception.
4. Read (or reread) the manual for the modules and methods 
giving you trouble.  From 
http://docs.python.org/lib/module-copy.html and from
http://www.python.org/doc/2.3/lib/module-copy.html:
"This version does not copy types like module, class, function..."


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1277718&group_id=5470


More information about the Python-bugs-list mailing list