Challenge supporting custom deepcopy with inheritance

Lie Ryan lie.1296 at
Tue Jun 2 06:34:43 EDT 2009

Gabriel Genellina wrote:
> En Mon, 01 Jun 2009 14:19:19 -0300, Michael H. Goldwasser
> <goldwamh at> escribió:
>>   I can examine the inherited slots to see which special methods are
>>   there, and to implement my own __deepcopy__ accordingly. But to do
>>   so well seems to essentially require reimplementing the complicated
>>   logic of the copy.deepcopy function.  That is, if my new class is
>>   the first to be implementing an explicit __deepcopy__ function, I
>>   seem to have no easy way to invoke the inherited version of
>>   "deepcopy(self)".
> Yes, that's a problem. But there is a workaround: since __deepcopy__ is
> searched *in the instance* (unlike many other __special__ methods, that
> are usually searched in the class itself) you can fool the copy logic
> into thinking there is no __deepcopy__ method defined, just by
> (temporarily) setting an instance attribute __deepcopy__ to None. (It's
> a hack, anyway)

I've never really used pickle before but maybe you could try pickling
then unpickling? It is a hack, but for some objects that does not have
__deepcopy__ it might be sufficient.

More information about the Python-list mailing list