Legitimacy of deepcopy

Scott David Daniels Scott.Daniels at Acm.Org
Fri Jun 4 12:32:30 EDT 2004


Eugeni Doljenko wrote:

> There is a list of custom objects. I want do duplicate this list to modify
> objects in new list and then compare old and new. I could do it with
> deepcopy
> 
> class foo:
>     def __init__(self, str):
>         self.str = str
> old = [foo('str1'), foo('str2')]
> 
> import copy
> new = copy.deepcopy(old)
> 
> But I've found very few deepcopy uses in Python library and other programs,
> so I wonder it's possible to do it other, more elegant way.

The problem with deepcopy is that it is impossible to do "correctly,"
since "correctly" depends on where your data structure abstractions are.
A simple example:

   steve = People('Steve')
   joe = People(steve)
   mystruct = [(steve, joe, Dollars(5)), steve]

Now, should a deepcopy have two distinct steves, or one?  Is the $5.00
in the new structure "the same as" another $5.00 or not?  Sometime you
mean the identity of an object, and sometimes you mean it as a value,
and no general purpose function named deepcopy can guess where to stop
copying.

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list