468,170 Members | 2,046 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,170 developers. It's quick & easy.

deepcopy raises TypeError for method/function?

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
Jul 18 '05 #1
1 1710
OKB (not okblacke) <Br******@aol.com> wrote:
...
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?


You can use copy_reg to register an identity function as the way to
'copy' function and methods, if that floats your boat. And sure, there
are other types that raise errors, imagine copying a file object, a
socket, ...!
Alex
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by 5HH575-UAZWKVVP-7H2H48V3 | last post: by
reply views Thread by phil | last post: by
6 posts views Thread by phil | last post: by
reply views Thread by Joshua Ginsberg | last post: by
7 posts views Thread by Alexandre Guimond | last post: by
3 posts views Thread by none | last post: by
reply views Thread by Robin Becker | last post: by
1 post views Thread by Wouter DW | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.