Issue #7092: Fix the DeprecationWarnings emitted by the standard library
[python.git] / Doc / library / copy.rst
blobb3ce51f57e40dc61ec0d9fdbd666bab2280d1f03
1 :mod:`copy` --- Shallow and deep copy operations
2 ================================================
4 .. module:: copy
5    :synopsis: Shallow and deep copy operations.
7 This module provides generic (shallow and deep) copying operations.
10 Interface summary:
12 .. function:: copy(x)
14    Return a shallow copy of *x*.
17 .. function:: deepcopy(x)
19    Return a deep copy of *x*.
22 .. exception:: error
24    Raised for module specific errors.
27 The difference between shallow and deep copying is only relevant for compound
28 objects (objects that contain other objects, like lists or class instances):
30 * A *shallow copy* constructs a new compound object and then (to the extent
31   possible) inserts *references* into it to the objects found in the original.
33 * A *deep copy* constructs a new compound object and then, recursively, inserts
34   *copies* into it of the objects found in the original.
36 Two problems often exist with deep copy operations that don't exist with shallow
37 copy operations:
39 * Recursive objects (compound objects that, directly or indirectly, contain a
40   reference to themselves) may cause a recursive loop.
42 * Because deep copy copies *everything* it may copy too much, e.g.,
43   administrative data structures that should be shared even between copies.
45 The :func:`deepcopy` function avoids these problems by:
47 * keeping a "memo" dictionary of objects already copied during the current
48   copying pass; and
50 * letting user-defined classes override the copying operation or the set of
51   components copied.
53 This module does not copy types like module, method, stack trace, stack frame,
54 file, socket, window, array, or any similar types.  It does "copy" functions and
55 classes (shallow and deeply), by returning the original object unchanged; this
56 is compatible with the way these are treated by the :mod:`pickle` module.
58 Shallow copies of dictionaries can be made using :meth:`dict.copy`, and
59 of lists by assigning a slice of the entire list, for example,
60 ``copied_list = original_list[:]``.
62 .. versionchanged:: 2.5
63    Added copying functions.
65 .. index:: module: pickle
67 Classes can use the same interfaces to control copying that they use to control
68 pickling.  See the description of module :mod:`pickle` for information on these
69 methods.  The :mod:`copy` module does not use the :mod:`copy_reg` registration
70 module.
72 .. index::
73    single: __copy__() (copy protocol)
74    single: __deepcopy__() (copy protocol)
76 In order for a class to define its own copy implementation, it can define
77 special methods :meth:`__copy__` and :meth:`__deepcopy__`.  The former is called
78 to implement the shallow copy operation; no additional arguments are passed.
79 The latter is called to implement the deep copy operation; it is passed one
80 argument, the memo dictionary.  If the :meth:`__deepcopy__` implementation needs
81 to make a deep copy of a component, it should call the :func:`deepcopy` function
82 with the component as first argument and the memo dictionary as second argument.
85 .. seealso::
87    Module :mod:`pickle`
88       Discussion of the special methods used to support object state retrieval and
89       restoration.