[Python-ideas] Being able to specify "copy mode" to copy.deepcopy
Vitor Bosshard
algorias at gmail.com
Tue Dec 15 17:19:43 CET 2009
2009/12/15 Ram Rachum <cool-rr at cool-rr.com>:
> MRAB <python at ...> writes:
>> cool-RR wrote:
>> > 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).
>
>
> I agree it that the Persistent.__deecopy__ thing does smell like misuse on my
> part. However I'd be happy to hear any alternative suggestion you have on how
> to solve the problem I have.
Deepcopy is a very simple operation conceptually, there's no need to
make it more complicated. How about implementing __deepcopy__ in your
world state objects? Specify attributes that don't need copying. You
can even use the Persistent class to signal that. Something like this
(untested!):
def __deepcopy__(self):
new = self.__class__()
for k,v in self.__dict__.iteritems():
setattr(new, k, v if isinstance(v, Persistent) else deepcopy(v))
return new
Vitor
More information about the Python-ideas
mailing list