blob: b3ce51f57e40dc61ec0d9fdbd666bab2280d1f03 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`copy` --- Shallow and deep copy operations
2================================================
3
4.. module:: copy
5 :synopsis: Shallow and deep copy operations.
6
Georg Brandl8ec7f652007-08-15 14:28:01 +00007This module provides generic (shallow and deep) copying operations.
8
Georg Brandl8ec7f652007-08-15 14:28:01 +00009
Georg Brandlffdde9e2009-09-09 16:49:13 +000010Interface summary:
Georg Brandl8ec7f652007-08-15 14:28:01 +000011
Georg Brandlffdde9e2009-09-09 16:49:13 +000012.. function:: copy(x)
Georg Brandl8ec7f652007-08-15 14:28:01 +000013
Georg Brandlffdde9e2009-09-09 16:49:13 +000014 Return a shallow copy of *x*.
15
16
17.. function:: deepcopy(x)
18
19 Return a deep copy of *x*.
20
21
22.. exception:: error
23
24 Raised for module specific errors.
25
Georg Brandl8ec7f652007-08-15 14:28:01 +000026
Georg Brandl8ec7f652007-08-15 14:28:01 +000027The difference between shallow and deep copying is only relevant for compound
28objects (objects that contain other objects, like lists or class instances):
29
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.
32
33* A *deep copy* constructs a new compound object and then, recursively, inserts
34 *copies* into it of the objects found in the original.
35
36Two problems often exist with deep copy operations that don't exist with shallow
37copy operations:
38
39* Recursive objects (compound objects that, directly or indirectly, contain a
40 reference to themselves) may cause a recursive loop.
41
42* Because deep copy copies *everything* it may copy too much, e.g.,
43 administrative data structures that should be shared even between copies.
44
45The :func:`deepcopy` function avoids these problems by:
46
47* keeping a "memo" dictionary of objects already copied during the current
48 copying pass; and
49
50* letting user-defined classes override the copying operation or the set of
51 components copied.
52
53This module does not copy types like module, method, stack trace, stack frame,
54file, socket, window, array, or any similar types. It does "copy" functions and
55classes (shallow and deeply), by returning the original object unchanged; this
56is compatible with the way these are treated by the :mod:`pickle` module.
57
Mark Summerfieldfcb444a2007-09-04 08:16:15 +000058Shallow copies of dictionaries can be made using :meth:`dict.copy`, and
59of lists by assigning a slice of the entire list, for example,
60``copied_list = original_list[:]``.
61
Georg Brandl8ec7f652007-08-15 14:28:01 +000062.. versionchanged:: 2.5
63 Added copying functions.
64
65.. index:: module: pickle
66
67Classes can use the same interfaces to control copying that they use to control
68pickling. See the description of module :mod:`pickle` for information on these
Georg Brandldffbf5f2008-05-20 07:49:57 +000069methods. The :mod:`copy` module does not use the :mod:`copy_reg` registration
Georg Brandl8ec7f652007-08-15 14:28:01 +000070module.
71
72.. index::
73 single: __copy__() (copy protocol)
74 single: __deepcopy__() (copy protocol)
75
76In order for a class to define its own copy implementation, it can define
77special methods :meth:`__copy__` and :meth:`__deepcopy__`. The former is called
78to implement the shallow copy operation; no additional arguments are passed.
79The latter is called to implement the deep copy operation; it is passed one
80argument, the memo dictionary. If the :meth:`__deepcopy__` implementation needs
81to make a deep copy of a component, it should call the :func:`deepcopy` function
82with the component as first argument and the memo dictionary as second argument.
83
84
85.. seealso::
86
87 Module :mod:`pickle`
88 Discussion of the special methods used to support object state retrieval and
89 restoration.
90