deepcopy raises TypeError for method/function?
OKB (not okblacke)
BrenBarn at aol.com
Thu Sep 9 23:04:54 EDT 2004
Alex Martelli wrote:
>> 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, ...!
Wasn't someone just saying on another thread, though, that copy
doesn't "officially" use copy_reg?
Anyway, I guess my real question is, is there any safe way to do
something like
for a in someList:
b = copy.deepcopy(a)
. . . without having to know ahead of time what kinds of objects
are in someList? I could maybe use try/except to catch failure, but if
deepcopy fails like it did with methods, will it always throw a
TypeError, or might it raise some other kind of error? Alternatively,
is there a way to tell without actually attempting the deepcopy whether
it will be able to copy the object or not?
--
--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