Say you want to copy an object. If the object is just a primitive value, depending on the language in question, just assigning a new variable to this value will copy everything about it. For example, in Java
int a = 5; int b = a; copies the value
5. This is pretty straightforward.
With non-primitive objects, the situation gets more nuanced. For example, in Python
a = [, [1, 2], [1, 2, 3]]
b = a
just points the variable
b towards the same object as
a, so nothing is copied. However, you can do the following in Python to perform a shallow copy:
b = copy.copy(a)
id(a) == id(b) # returns False, since b is a new object
id(a) == id(b) # returns True. a and b are the same object
That 3rd line is the reason the copy is ‘shallow’: a new object was created to assign
b to, but the attributes of
b (its values) are the same ones as in
Enter the ‘deep copy’, which does pretty much what it sounds like. A deep copy not only creates a new object, but recursively creates new objects copying that object’s attributes. Thus it’s not only just a copied wrapper, but a real copy all the way down.
To contrast with the above example:
b = copy.deepcopy(a)
id(a) == id(b) # returns False, as with the shallow copy
id(a) == id(b) # now returns False. b is a new value separate from a
To summarize, the way the Python docs summarize the difference is:
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
- A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
- A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Finally, as with any recursive procedure, deep copying should be done carefully to avoid infinite loops and other problems.