1:mod:`copy` --- Shallow and deep copy operations 2================================================ 3 4.. module:: copy 5 :synopsis: Shallow and deep copy operations. 6 7Assignment statements in Python do not copy objects, they create bindings 8between a target and an object. For collections that are mutable or contain 9mutable items, a copy is sometimes needed so one can change one copy without 10changing the other. This module provides generic shallow and deep copy 11operations (explained below). 12 13 14Interface summary: 15 16.. function:: copy(x) 17 18 Return a shallow copy of *x*. 19 20 21.. function:: deepcopy(x) 22 23 Return a deep copy of *x*. 24 25 26.. exception:: error 27 28 Raised for module specific errors. 29 30 31The difference between shallow and deep copying is only relevant for compound 32objects (objects that contain other objects, like lists or class instances): 33 34* A *shallow copy* constructs a new compound object and then (to the extent 35 possible) inserts *references* into it to the objects found in the original. 36 37* A *deep copy* constructs a new compound object and then, recursively, inserts 38 *copies* into it of the objects found in the original. 39 40Two problems often exist with deep copy operations that don't exist with shallow 41copy operations: 42 43* Recursive objects (compound objects that, directly or indirectly, contain a 44 reference to themselves) may cause a recursive loop. 45 46* Because deep copy copies everything it may copy too much, such as data 47 which is intended to be shared between copies. 48 49The :func:`deepcopy` function avoids these problems by: 50 51* keeping a "memo" dictionary of objects already copied during the current 52 copying pass; and 53 54* letting user-defined classes override the copying operation or the set of 55 components copied. 56 57This module does not copy types like module, method, stack trace, stack frame, 58file, socket, window, array, or any similar types. It does "copy" functions and 59classes (shallow and deeply), by returning the original object unchanged; this 60is compatible with the way these are treated by the :mod:`pickle` module. 61 62Shallow copies of dictionaries can be made using :meth:`dict.copy`, and 63of lists by assigning a slice of the entire list, for example, 64``copied_list = original_list[:]``. 65 66.. versionchanged:: 2.5 67 Added copying functions. 68 69.. index:: module: pickle 70 71Classes can use the same interfaces to control copying that they use to control 72pickling. See the description of module :mod:`pickle` for information on these 73methods. The :mod:`copy` module does not use the :mod:`copy_reg` registration 74module. 75 76.. index:: 77 single: __copy__() (copy protocol) 78 single: __deepcopy__() (copy protocol) 79 80In order for a class to define its own copy implementation, it can define 81special methods :meth:`__copy__` and :meth:`__deepcopy__`. The former is called 82to implement the shallow copy operation; no additional arguments are passed. 83The latter is called to implement the deep copy operation; it is passed one 84argument, the memo dictionary. If the :meth:`__deepcopy__` implementation needs 85to make a deep copy of a component, it should call the :func:`deepcopy` function 86with the component as first argument and the memo dictionary as second argument. 87 88 89.. seealso:: 90 91 Module :mod:`pickle` 92 Discussion of the special methods used to support object state retrieval and 93 restoration. 94 95