blob: a8e8bfb1e832bb2dc569ff04edd27c731a28b3f0 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`copy` --- Shallow and deep copy operations
2================================================
3
4.. module:: copy
5 :synopsis: Shallow and deep copy operations.
6
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/copy.py`
8
9--------------
10
Senthil Kumaran5b14d732012-02-09 18:26:59 +080011Assignment statements in Python do not copy objects, they create bindings
12between a target and an object. For collections that are mutable or contain
13mutable items, a copy is sometimes needed so one can change one copy without
14changing the other. This module provides generic shallow and deep copy
15operations (explained below).
Georg Brandl116aa622007-08-15 14:28:22 +000016
Georg Brandl116aa622007-08-15 14:28:22 +000017
Georg Brandla59750f2009-09-09 16:51:05 +000018Interface summary:
Georg Brandl116aa622007-08-15 14:28:22 +000019
Georg Brandla59750f2009-09-09 16:51:05 +000020.. function:: copy(x)
Georg Brandl116aa622007-08-15 14:28:22 +000021
Georg Brandla59750f2009-09-09 16:51:05 +000022 Return a shallow copy of *x*.
23
24
Stephan Hoyer02009282018-10-29 11:30:12 -070025.. function:: deepcopy(x[, memo])
Georg Brandla59750f2009-09-09 16:51:05 +000026
27 Return a deep copy of *x*.
28
29
30.. exception:: error
31
32 Raised for module specific errors.
33
Raymond Hettinger69ee87e2019-08-24 11:15:44 -070034.. _shallow_vs_deep_copy:
Georg Brandl116aa622007-08-15 14:28:22 +000035
Georg Brandl116aa622007-08-15 14:28:22 +000036The difference between shallow and deep copying is only relevant for compound
37objects (objects that contain other objects, like lists or class instances):
38
39* A *shallow copy* constructs a new compound object and then (to the extent
40 possible) inserts *references* into it to the objects found in the original.
41
42* A *deep copy* constructs a new compound object and then, recursively, inserts
43 *copies* into it of the objects found in the original.
44
45Two problems often exist with deep copy operations that don't exist with shallow
46copy operations:
47
48* Recursive objects (compound objects that, directly or indirectly, contain a
49 reference to themselves) may cause a recursive loop.
50
Sanyam Khurana19e04942017-04-09 15:52:30 +053051* Because deep copy copies everything it may copy too much, such as data
52 which is intended to be shared between copies.
Georg Brandl116aa622007-08-15 14:28:22 +000053
54The :func:`deepcopy` function avoids these problems by:
55
Stephan Hoyer02009282018-10-29 11:30:12 -070056* keeping a ``memo`` dictionary of objects already copied during the current
Georg Brandl116aa622007-08-15 14:28:22 +000057 copying pass; and
58
59* letting user-defined classes override the copying operation or the set of
60 components copied.
61
62This module does not copy types like module, method, stack trace, stack frame,
63file, socket, window, array, or any similar types. It does "copy" functions and
64classes (shallow and deeply), by returning the original object unchanged; this
65is compatible with the way these are treated by the :mod:`pickle` module.
66
Thomas Wouters89d996e2007-09-08 17:39:28 +000067Shallow copies of dictionaries can be made using :meth:`dict.copy`, and
68of lists by assigning a slice of the entire list, for example,
69``copied_list = original_list[:]``.
70
Georg Brandl116aa622007-08-15 14:28:22 +000071.. index:: module: pickle
72
73Classes can use the same interfaces to control copying that they use to control
74pickling. See the description of module :mod:`pickle` for information on these
Martin Panter2e4571a2015-11-14 01:07:43 +000075methods. In fact, the :mod:`copy` module uses the registered
76pickle functions from the :mod:`copyreg` module.
Georg Brandl116aa622007-08-15 14:28:22 +000077
78.. index::
79 single: __copy__() (copy protocol)
80 single: __deepcopy__() (copy protocol)
81
82In order for a class to define its own copy implementation, it can define
83special methods :meth:`__copy__` and :meth:`__deepcopy__`. The former is called
84to implement the shallow copy operation; no additional arguments are passed.
85The latter is called to implement the deep copy operation; it is passed one
Stephan Hoyer02009282018-10-29 11:30:12 -070086argument, the ``memo`` dictionary. If the :meth:`__deepcopy__` implementation needs
Georg Brandl116aa622007-08-15 14:28:22 +000087to make a deep copy of a component, it should call the :func:`deepcopy` function
88with the component as first argument and the memo dictionary as second argument.
89
90
91.. seealso::
92
93 Module :mod:`pickle`
94 Discussion of the special methods used to support object state retrieval and
95 restoration.
96