deepcopy raises TypeError for method/function?

Alex Martelli aleaxit at yahoo.com
Fri Sep 10 02:17:44 EDT 2004


OKB (not okblacke) <BrenBarn at aol.com> wrote:

> 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?

Right, it was me -- I was carping about the inconsistency between [a]
claiming that if something ain't in the docs it don't count, [b] telling
people to look in the sources as the ultimate reference.  We're doing
both things, holding both contradictory stances at once, and I don't
like that:-(.  This one bug in the docs we'll fix, of course, but the
overall strategy remains a problem.

> 
>       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

Failure is surely possible, since not all types are copyable.  Yes, you
can use a try/except -- but if you enter the except clause the copy
won't have happened, so if you need it you're in trouble.

> deepcopy fails like it did with methods, will it always throw a 
> TypeError, or might it raise some other kind of error?  Alternatively,

TypeError would be normal, but I don't think it can be guaranteed.
Consider...:

In [1]: class X(object):
   ...:     def __init__(self): self.xxx = 44
   ...:     def __getstate__(self): return self.xx  # oops
   ...:     

Here, a copy.deepcopy(X()) will end with AttributeError, because of the
oops... for different kind of oopses you could surely have all kinds of
errors.  'except Exception:" thus seems sounder.

> is there a way to tell without actually attempting the deepcopy whether
> it will be able to copy the object or not?

None I can think of, sorry.


Alex



More information about the Python-list mailing list