[Python-ideas] Being able to specify "copy mode" to copy.deepcopy

cool-RR cool-rr at cool-rr.com
Tue Dec 15 13:36:46 CET 2009


This is about the `copy.deepcopy` function.

With the __deepcopy__ method, user-defined objects can specify how they will
be copied. But it is assumed that you will always want to copy them the same
way. What if sometimes you want to copy them in one way and sometimes in
another?

I am now being held back by this limitation. I will give some background to
what I'm doing:

I'm developing a simulations framework called GarlicSim. You can see a short
video here:
http://garlicsim.org/brief_introduction.html
The program handles world states in simulated worlds. To generate the next
world state in the timeline, the last world state is deepcopied and then
modified.

Now sometimes in simulations there are big, read-only objects that I don't
want to replicate for each world state. For example, a map of the
environment in which the simulation takes place. So I have defined a class
called `Persistent`, for which I have defined a __deepcopy__ that doesn't
actually copy it, but gives a reference to the original object. So now I can
use `Persistent` as a sub-class to these big objects that I don't want to
replicate.

But in some cases I do want to replicate these objects, and I can't!

So I suggest that it will be possible to specify a "mode" for copying. User
defined objects will be able to specify how they will be deepcopied in each
mode.

What do you think?


Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091215/244edf7e/attachment.html>


More information about the Python-ideas mailing list