deepcopy questions

L

lars van gemerden

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
 
S

Steven D'Aprano

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__:

What makes you think that this is a strange result? What result are you
expecting?
{'_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?

The most obvious guess is that the memo dict already contains _items_ and
_names_, and so they get skipped.

Please ensure your sample code can be run. You should create the simplest
example of stand-alone code that other people can run. See more
information here:

http://sscce.org/


By the way, is it just me or is the documentation for deepcopy seriously
lacking? http://docs.python.org/3/library/copy.html

There's no mention of the additional arguments memo and _nil, and while
the docs say to pass the memo dictionary to __deepcopy__ it doesn't
document any restrictions on this memo, how to initialise it, or under
what circumstances you would pass anything but an empty dict.
 
M

MRAB

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?
I haven't been able to reproduce the problem.

Could you provide a self-contained and _runnable_ piece of code that
shows the problem?
 
L

lars van gemerden

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 insteadbe 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
 
D

Dieter Maurer

lars van gemerden said:
... "deepcopy" dropping some items ...
Any ideas are still more then welcome,

"deepcopy" is implemented in Python (rather than "C").
Thus, if necessary, you can debug what it is doing
and thereby determine where the items have been dropped.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top