Challenge supporting custom deepcopy with inheritance

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jun 2 23:23:03 CEST 2009


En Tue, 02 Jun 2009 07:34:43 -0300, Lie Ryan <lie.1296 at gmail.com> escribió:

> Gabriel Genellina wrote:
>> En Mon, 01 Jun 2009 14:19:19 -0300, Michael H. Goldwasser
>> <goldwamh at slu.edu> 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.

deepcopy essencially does that, without the intermediate storage.
The problem is, how to customize deepcopy(something) in a derived class,  
if there is no way to call the inherited behavior from its base class.

-- 
Gabriel Genellina




More information about the Python-list mailing list