semi-deepcopy?

Steven Knight knight at baldmt.com
Sun Feb 25 15:45:47 EST 2001


Brian Quinlan wrote:

> I can't claim to have tried this or completely thought it through, but you
> could probably install your own copy handler for undeepcopyable types to
> accomplish the behavior that you want i.e.
>
> import copy
> import types
>
> copy._deepcopy_dispatch[types.ModuleType] = copy._copy_atomic
> # Put all of the other types that you need here...

Beautiful.  I found two wrinkles:

copy._copy_atomic only takes one argument, but copy.deepcopy passes its memo
variable as a second argument.  Easily taken care of by using my own glue
function instead.

There are other places where I'll still want to use the existing
copy.deepcopy,
so globally changing its semantics this way is overkill.  Also easily handled

(at the expense of a little thread-unsafe overhead) by saving/restoring my
own
_deepcopy_dispatch list.

Many thanks.

    --SK


> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Steven Knight
> Sent: Sunday, February 25, 2001 10:31 AM
> To: python-list at python.org
> Subject: semi-deepcopy?
>
> I need to make a deep copy of an arbitrarily complicated data structure,
> e.g., a dictionary where the values are lists of objects that may have
> arbitrarily complicated attributes (dictionaries, lists, etc.).
>
> The problem is that elements down in the depths of the structure may be
> things like functions or other un-deep-copyable objects.  This makes
> copy.deepcopy() throw the exception you'd expect.
>
> What I think I need is a semi-deep (semi-shallow?) copy that performs
> a deepcopy when the object supports it, but just copies a reference to
> objects that don't.  Sharing references to those objects is fine, but I
> do still need to preserve copies of all the other mutable objects in the
> structure.
>
> I can't believe I'm the first person to need this functionality, but I
> don't see anything like this available.  Am I overlooking some other
> way to do what I want, or will I have to roll my own solution here?




More information about the Python-list mailing list