copy()
Introduction
You can read the theory of deep and shallow copies in the article
«Types of copying»
The source code of the function can be studied
here
"""Generic (shallow and deep) copying operations. Interface summary: import copy x = copy.copy(y) # make a shallow copy of y x = copy.deepcopy(y) # make a deep copy of y For module specific errors, copy.Error is raised. 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 the same objects that the original contains. - A deep copy constructs a new compound object and then, recursively, inserts copies of the objects found in the original. Two problems often exist with deep copy operations that do not exist with shallow copy operations: a) recursive objects (compound objects that, directly or indirectly, contain a reference to themselves) may cause a recursive loop b) because deep copy copies everything it may copy too much, such as administrative data structures that should be shared even between copies The deepcopy operation avoids these problems by: a) keeping a memo dictionary of objects already copied during the current copying pass b) letting user-defined classes override the copying operation or the set of components copied This version does not copy types like a module, class, function, method, nor a stack trace, stack frame, file, socket, window, nor any similar types. Classes can use the same interfaces to control copying that they use to control pickling: they can define methods called __getinitargs__(), __getstate__() and __setstate__(). See the documentation of the "pickle" module for information on these methods. """
Syntax
copy(object)
Example
import copy z = [2] y = z x = copy.copy(z) z.append(3) print("z =", z) print("y =", y) print("x =", x)
z = [2, 3] y = [2, 3] x = [2]
The point is that the list x did not change because copy() created a new object rather than just a reference to the old one.
Usage
Consider the modify() function from the article about Objects in Python
m = [0, 1, 2] print(f"m = {m}") def modify(k): k.append(3) print("k =", k) print("\nmodify(m)\n") modify(m) print(f"m = {m}") print("\nNo copy of m is made\n") print("\nmodify(m)\n") modify(m) print(f"m = {m}")
m = [0, 1, 2] modify(m) k = [0, 1, 2, 3] m = [0, 1, 2, 3] No copy of m is made modify(m) k = [0, 1, 2, 3, 3] m = [0, 1, 2, 3, 3]
The function modify() works with the same object, so its values change.
You can change the behaviour of modify() with the copy() function
import copy m = [0, 1, 2] print(f"m = {m}") def modify(k): s = copy.copy(k) s.append(3) print("s =", s) print("\nmodify(m)\n") modify(m) print(f"m = {m}") print("\nNo copy of m is made\n") print("\nmodify(m)\n") modify(m) print(f"m = {m}")
m = [0, 1, 2] modify(m) s = [0, 1, 2, 3] m = [0, 1, 2] No copy of m is made modify(m) s = [0, 1, 2, 3] m = [0, 1, 2]
Article author: Andrei Olegovich