I am trying to initialize a list of lists representing a 3x3 array:
import copy
m = copy.deepcopy(3*[3*[0]])
print(m)
m[1][2] = 100
print(m)
and the output is:
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 0, 100], [0, 0, 100], [0, 0, 100]]
which is not what I expected since the last elements of each row are shared! I did get the result I need by using:
m = [ copy.deepcopy(3*[0]) for i in range(3) ]
but I don't understand why the first (and simpler) form does not work. Isn't deepcopy
supposed to be deep?
The thing is that you create a list of 3 times the same object, so when you assign a value in one of the lists, it affects all of them (because it is the same).
Try to do:
Here you create "a", which is a list of 3 lists of size 3, initialized with 0's. Deep copying "a" will give you "m" - the same as "a", but different object, so that changing "a" will not affect "m" and vice versa.
The problem is that
deepcopy
keeps amemo
that contains all instances that have been copied already. That's to avoid infinite recursions and intentional shared objects. So when it tries to deepcopy the second sublist it sees that it has already copied it (the first sublist) and just inserts the first sublist again. In shortdeepcopy
doesn't solve the "shared sublist" problem!To quote the documentation:
(emphasis mine)
That means that
deepcopy
regards shared references as intention. For example consider the class:Neglecting that the class doesn't make much sense as is it intentionally shares the references between its
x
andx1
andx2
attribute. It would be weird ifdeepcopy
broke those shared references by doing a separate copy of each of these. That's why the documentation mentions this as a "solution" to the problem of "copy too much, such as data which is intended to be shared between copies.".Back to your example: If you don't want to have shared references it would be better to avoid them completely:
In your case the inner elements are immutable because
0
is immutable - but if you deal with mutable instances inside the innermost lists you must have to avoid the inner list multiplication as well:After I read several answers I thought a bit more about this question. The problem lies in the fact that there is no way that a recursive (circular) object can be deeply copied! For instance:
produces an object which behaves like an infinite sequence of 1's which Python prints as:
The same happens to
deepcopy(x)
. In my opinion Python implementation adopted a solution which avoids infinite loops but may produce incorrect results for objects without circularities but with shared components. I'd rather see my program loop forever and fix it than have to search for an obscure bug!