Python copy.deepcopy() function not working properly [duplicate]

孤人 提交于 2019-12-20 02:34:27

问题


I have been playing with the deepcopy function and the copy function and I get the same issue with both of them. It is like the copy was a reference (or a pointer) instead of a proper copy. I am working with data records (classes) in Python, maybe it could be that.. I show you an example:

>>> import copy
>>> class player1:
...    age = 23
...    score = 1
>>> class player2:
...    age = 14
...    score = 2
>>> player3 = copy.deepcopy(player1)

I print the parameters.

>>> print player1.age, player1.score
23 1
>>> print player2.age, player2.score
14 2
>>> print player3.age, player3.score
23 1

Now I increment the score parameter in player1 data record.

>>> player1.score += 3

And I print the results again.

>>> print player1.age, player1.score
23 4
>>> print player2.age, player2.score
14 2
>>> print player3.age, player3.score
23 4 

WHY HAS PLAYER 3 CHANGED? I just incremented a parameter in player1, not player3. It is mutable instead of immutable.

Thanks in advance.


回答1:


The problem is that you are actually copying the class definition and not an instance of the class.

Another problem of the code is that the attributes age and score are part of the class and will be shared between all instances of that class. This is probably not what you intended.

What you probably want to do is:

import copy
class Player:
    def __init__(self, age, score):
        self.age = age
        self.score = score

player1 = Player(23, 1)
player2 = Player(14, 2)
player3 = copy.deepcopy(player1)

player1.age += 1

print "player1.age", player1.age
print "player3.age", player3.age

This gives you what you expect:

player1.age 24
player3.age 23



回答2:


From the documentation (emphasis mine):

This version does not copy types like module, class, function, method, nor stack trace, stack frame, nor file, socket, window, nor array, nor any similar types.

You're trying to copy classes, and so:

>>> player3 = copy.deepcopy(player1)
>>> player1 is player3
True

but

>>> p1 = player1()
>>> p2 = player2()
>>> p3 = copy.deepcopy(p1)
>>> p1 is p3
False



回答3:


This is working as designed.

In your sample code, you are copying class definitions, not object instances. From copy module manual page:

It does “copy” functions and classes (shallow and deeply), by returning the original object
unchanged

Hence:

player3 = copy.deepcopy(player1)

is the same as:

player3 = player1

However, if you copied instances of the classes, you would get the expected result:

player3 = copy.deepcopy(player1())


来源:https://stackoverflow.com/questions/22079916/python-copy-deepcopy-function-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!