deepcopy raises TypeError for method/function?

OKB (not okblacke) BrenBarn at aol.com
Thu Sep 9 13:58:56 EDT 2004


    	I've noticed some peculiar behavior from copy.deepcopy: if you pass 
it a method or function object, it raises a TypeError somewhere in 
copy_reg saying "function() takes at least 2 arguments (0 given)".  I'm 
guessing this is the constructor for functions, and it's somehow being 
incorrectly called, but I'm not really sure.

    	This behavior is annoying, however.  I realize the deepcopy 
documentation warns that it doesn't handle copying certain things, but 
its behavior when asked to copy those things varies.  For instance, 
deepcopy(SomeClass) just silently returns the same SomeClass object.  I 
would expect deepcopy(someObj.someMethod) to do likewise, just returning 
the same method object.

    	Does anyone have any pointers on this?  It seems that certain types 
are safe to deepcopy (even if they're not actually copied) whereas 
others are not.  Do I just have to manually typecheck for functions and 
methods?  Are there any other types that might raise such an error?

-- 
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail."
	--author unknown



More information about the Python-list mailing list