Copying objects and multiple inheritance

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Jun 3 22:50:32 CEST 2009


En Wed, 03 Jun 2009 10:20:35 -0300, Brian Allen Vanderburg II  
<BrianVanderburg2 at aim.com> escribió:
> Gabriel Genellina wrote:
>> En Tue, 02 Jun 2009 19:02:47 -0300, Brian Allen Vanderburg II  
>> <BrianVanderburg2 at aim.com> escribió:
>>
>>> What is the best way to copy an object that has multiple inheritance  
>>> with the copy module.  Particularly, some of the instances in the  
>>> hierarchy
>>> use the __copy__ method to create a copy (because even for shallow  
>>> copies they need some information  updated a little differently)
>>
> I do control the classes involved.  A problem I was having, but I think  
> I now got solved, is if using super, the copy would not have the same  
> class type.

Normally, one would use type(self)(...init arguments...) to create an  
instance of the same type. But if the __init__ signature may change, use  
type(self).__new__(type(self)) to bypass __init__ (should not matter in  
this case).

> Also, a problem was if using super, but some class in the hierarchy  
> didn't implement __copy__, then it's data would not be copied at all.   
> This was also fixed by copying the entire __dict__ in the base  
> __copy__.  This is an idea of what I got, it seems to be working fine:

Looks fine. See also a very recent thread about __deepcopy__ for another  
generic version.

-- 
Gabriel Genellina




More information about the Python-list mailing list