deepcopy questions

lars van gemerden lars at rational-it.com
Wed Nov 28 04:05:20 EST 2012


On Wednesday, November 28, 2012 12:59:38 AM UTC+1, lars van gemerden wrote:
> Hi,
> 
> 
> 
> I get a very strange result when using deepcopy. The following code:
> 
> 
> 
>     def __deepcopy__(self, memo):
> 
>         independent = self.independent()
> 
>         if independent is self:
> 
>             out = type(self)()
> 
>             out.__dict__ = copy.deepcopy(self.__dict__, memo)
> 
>             print self.__dict__
> 
>             print out.__dict__ #strange result
> 
>             return out
> 
>         else:
> 
>             return copy.deepcopy(independent, memo).find(self.id).take()
> 
> 
> 
> prints different results for self.__dict__ and out.__dict__:
> 
> 
> 
> {'_active_': False, 'init': {}, '_id_': 0, '_items_': [<flow.library.collector object at 0x03893910>], '_name_': 'main'} 
> 
> {'_active_': False, 'init': {}, '_id_': 0}
> 
> 
> 
> Two items are missing in the copy. Maybe i am missing something obvious, but i cannot figure out how this could happen.
> 
> 
> 
> Can anyone tell me how this is possible?
> 
> 
> 
> Cheers, Lars

I have just tried to make a simple runnable testcase but no luck. in my code it's part of a rather complex data structure. 

As I understood the documentation, the memo parameter is to hold a dictionary of data that have already been copied (e.g. to deal with circular references), and is normally only explicitly used when implementing __deepcopy__, just passing memo to calls to deepcopy within the body of __deepcopy__.

If memo contains items, this should, to my understanding, not remove them from the output of deepcopy, they will just not be copied again, but instead be taken from memo and put in the output (otherwise 2 references to the same object would after deepcopying result in 2 distinct copies of that object).

Anyway, since i cannot reproduce the error in a simple testcase and i have no adea what is going on, I'll implement what i need differently.

Any ideas are still more then welcome,

Thanks for the feedback,

Lars



More information about the Python-list mailing list