blob: 67cf65a8b250c7f300e20ce808ea25046b78cef2 [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
Senthil Kumaran59e7c8f2012-02-09 18:22:01 +08007Assignment 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).
Georg Brandl8ec7f652007-08-15 14:28:01 +000012
Georg Brandl8ec7f652007-08-15 14:28:01 +000013
Georg Brandlffdde9e2009-09-09 16:49:13 +000014Interface summary:
Georg Brandl8ec7f652007-08-15 14:28:01 +000015
Georg Brandlffdde9e2009-09-09 16:49:13 +000016.. function:: copy(x)
Georg Brandl8ec7f652007-08-15 14:28:01 +000017
Georg Brandlffdde9e2009-09-09 16:49:13 +000018 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
Georg Brandl8ec7f652007-08-15 14:28:01 +000030
Georg Brandl8ec7f652007-08-15 14:28:01 +000031The 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
Nick Coghlanab16dc12017-04-09 20:57:29 +100046* Because deep copy copies everything it may copy too much, such as data
47 which is intended to be shared between copies.
Georg Brandl8ec7f652007-08-15 14:28:01 +000048
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
Mark Summerfieldfcb444a2007-09-04 08:16:15 +000062Shallow 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
Georg Brandl8ec7f652007-08-15 14:28:01 +000066.. 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
Georg Brandldffbf5f2008-05-20 07:49:57 +000073methods. The :mod:`copy` module does not use the :mod:`copy_reg` registration
Georg Brandl8ec7f652007-08-15 14:28:01 +000074module.
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