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