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

MRAB python at mrabarnett.plus.com
Tue Dec 15 16:29:30 CET 2009


cool-RR wrote:
> 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?
> 
My own feeling is that this is a misuse of __deepcopy__: if you ask for
a copy (of a mutable object) then you should get a copy (for immutable
objects copying isn't necessary).



More information about the Python-ideas mailing list