blob: 14adbb9ce47856e68842c07851adc31869755f5b [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{copy} ---
2 Shallow and deep copy operations.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{copy}
4
5\modulesynopsis{Shallow and deep copy operations.}
6
Fred Drake3d299551998-11-30 18:59:44 +00007\withsubitem{(in module copy)}{%
8 \ttindex{copy}%
9 \ttindex{deepcopy}}
Guido van Rossumd1883581995-02-15 15:53:08 +000010
11This module provides generic (shallow and deep) copying operations.
12
13Interface summary:
14
Fred Drake19479911998-02-13 06:58:54 +000015\begin{verbatim}
Guido van Rossumd1883581995-02-15 15:53:08 +000016import copy
17
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000018x = copy.copy(y) # make a shallow copy of y
19x = copy.deepcopy(y) # make a deep copy of y
Fred Drake19479911998-02-13 06:58:54 +000020\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000021%
Fred Drake3d299551998-11-30 18:59:44 +000022For module specific errors, \exception{copy.error} is raised.
Guido van Rossumd1883581995-02-15 15:53:08 +000023
24The difference between shallow and deep copying is only relevant for
25compound objects (objects that contain other objects, like lists or
26class instances):
27
28\begin{itemize}
29
30\item
Fred Drakeaf8a0151998-01-14 14:51:31 +000031A \emph{shallow copy} constructs a new compound object and then (to the
32extent possible) inserts \emph{references} into it to the objects found
Guido van Rossumd1883581995-02-15 15:53:08 +000033in the original.
34
35\item
Fred Drakeaf8a0151998-01-14 14:51:31 +000036A \emph{deep copy} constructs a new compound object and then,
37recursively, inserts \emph{copies} into it of the objects found in the
Guido van Rossumd1883581995-02-15 15:53:08 +000038original.
39
40\end{itemize}
41
42Two problems often exist with deep copy operations that don't exist
43with shallow copy operations:
44
45\begin{itemize}
46
47\item
48Recursive objects (compound objects that, directly or indirectly,
49contain a reference to themselves) may cause a recursive loop.
50
51\item
Fred Drake3d299551998-11-30 18:59:44 +000052Because deep copy copies \emph{everything} it may copy too much,
53e.g., administrative data structures that should be shared even
54between copies.
Guido van Rossumd1883581995-02-15 15:53:08 +000055
56\end{itemize}
57
Fred Drake3d299551998-11-30 18:59:44 +000058The \function{deepcopy()} function avoids these problems by:
Guido van Rossumd1883581995-02-15 15:53:08 +000059
60\begin{itemize}
61
62\item
Guido van Rossumcf51dac1998-06-30 16:54:33 +000063keeping a ``memo'' dictionary of objects already copied during the current
Guido van Rossumd1883581995-02-15 15:53:08 +000064copying pass; and
65
66\item
67letting user-defined classes override the copying operation or the
68set of components copied.
69
70\end{itemize}
71
72This version does not copy types like module, class, function, method,
73nor stack trace, stack frame, nor file, socket, window, nor array, nor
74any similar types.
75
76Classes can use the same interfaces to control copying that they use
77to control pickling: they can define methods called
Fred Drake3d299551998-11-30 18:59:44 +000078\method{__getinitargs__()}, \method{__getstate__()} and
79\method{__setstate__()}. See the description of module
80\module{pickle}\refstmodindex{pickle} for information on these
81methods. The \module{copy} module does not use the \module{copy_reg}
82registration module.
83\withsubitem{(copy protocol)}{%
84 \ttindex{__getinitargs__()}%
85 \ttindex{__getstate__()}%
86 \ttindex{__setstate__()}}
Guido van Rossumcf51dac1998-06-30 16:54:33 +000087
88In order for a class to define its own copy implementation, it can
Fred Drake3d299551998-11-30 18:59:44 +000089define special methods \method{__copy__()} and
90\method{__deepcopy__()}. The former is called to implement the
91shallow copy operation; no additional arguments are passed. The
92latter is called to implement the deep copy operation; it is passed
93one argument, the memo dictionary. If the \method{__deepcopy__()}
94implementation needs to make a deep copy of a component, it should
95call the \function{deepcopy()} function with the component as first
96argument and the memo dictionary as second argument.
97\withsubitem{(copy protocol)}{%
98 \ttindex{__copy__()}%
99 \ttindex{__deepcopy__()}}
100
101\begin{seealso}
102\seemodule{pickle}{Discussion of the special disciplines used to
103support object state retrieval and restoration.}
104\end{seealso}