blob: ef42682b0aab51309a4e5199ca4335dc4b7f8cf9 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{copy} ---
Fred Drake2d2b6a01999-02-12 20:40:49 +00002 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
Guido van Rossumd1883581995-02-15 15:53:08 +00007
8This module provides generic (shallow and deep) copying operations.
Fred Drake2d2b6a01999-02-12 20:40:49 +00009\withsubitem{(in copy)}{\ttindex{copy()}\ttindex{deepcopy()}}
Guido van Rossumd1883581995-02-15 15:53:08 +000010
11Interface summary:
12
Fred Drake19479911998-02-13 06:58:54 +000013\begin{verbatim}
Guido van Rossumd1883581995-02-15 15:53:08 +000014import copy
15
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000016x = copy.copy(y) # make a shallow copy of y
17x = copy.deepcopy(y) # make a deep copy of y
Fred Drake19479911998-02-13 06:58:54 +000018\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000019%
Fred Drake3d299551998-11-30 18:59:44 +000020For module specific errors, \exception{copy.error} is raised.
Guido van Rossumd1883581995-02-15 15:53:08 +000021
22The difference between shallow and deep copying is only relevant for
23compound objects (objects that contain other objects, like lists or
24class instances):
25
26\begin{itemize}
27
28\item
Fred Drakeaf8a0151998-01-14 14:51:31 +000029A \emph{shallow copy} constructs a new compound object and then (to the
30extent possible) inserts \emph{references} into it to the objects found
Guido van Rossumd1883581995-02-15 15:53:08 +000031in the original.
32
33\item
Fred Drakeaf8a0151998-01-14 14:51:31 +000034A \emph{deep copy} constructs a new compound object and then,
35recursively, inserts \emph{copies} into it of the objects found in the
Guido van Rossumd1883581995-02-15 15:53:08 +000036original.
37
38\end{itemize}
39
40Two problems often exist with deep copy operations that don't exist
41with shallow copy operations:
42
43\begin{itemize}
44
45\item
46Recursive objects (compound objects that, directly or indirectly,
47contain a reference to themselves) may cause a recursive loop.
48
49\item
Fred Drake3d299551998-11-30 18:59:44 +000050Because deep copy copies \emph{everything} it may copy too much,
51e.g., administrative data structures that should be shared even
52between copies.
Guido van Rossumd1883581995-02-15 15:53:08 +000053
54\end{itemize}
55
Fred Drake3d299551998-11-30 18:59:44 +000056The \function{deepcopy()} function avoids these problems by:
Guido van Rossumd1883581995-02-15 15:53:08 +000057
58\begin{itemize}
59
60\item
Guido van Rossumcf51dac1998-06-30 16:54:33 +000061keeping a ``memo'' dictionary of objects already copied during the current
Guido van Rossumd1883581995-02-15 15:53:08 +000062copying pass; and
63
64\item
65letting user-defined classes override the copying operation or the
66set of components copied.
67
68\end{itemize}
69
70This version does not copy types like module, class, function, method,
71nor stack trace, stack frame, nor file, socket, window, nor array, nor
72any similar types.
73
74Classes can use the same interfaces to control copying that they use
75to control pickling: they can define methods called
Fred Drake3d299551998-11-30 18:59:44 +000076\method{__getinitargs__()}, \method{__getstate__()} and
77\method{__setstate__()}. See the description of module
Fred Drake2d2b6a01999-02-12 20:40:49 +000078\refmodule{pickle}\refstmodindex{pickle} for information on these
Fred Drake3d299551998-11-30 18:59:44 +000079methods. The \module{copy} module does not use the \module{copy_reg}
80registration module.
Fred Drake2d2b6a01999-02-12 20:40:49 +000081\withsubitem{(copy protocol)}{\ttindex{__getinitargs__()}
82 \ttindex{__getstate__()}\ttindex{__setstate__()}}
Guido van Rossumcf51dac1998-06-30 16:54:33 +000083
84In order for a class to define its own copy implementation, it can
Fred Drake3d299551998-11-30 18:59:44 +000085define special methods \method{__copy__()} and
86\method{__deepcopy__()}. The former is called to implement the
87shallow copy operation; no additional arguments are passed. The
88latter is called to implement the deep copy operation; it is passed
89one argument, the memo dictionary. If the \method{__deepcopy__()}
90implementation needs to make a deep copy of a component, it should
91call the \function{deepcopy()} function with the component as first
92argument and the memo dictionary as second argument.
Fred Drake2d2b6a01999-02-12 20:40:49 +000093\withsubitem{(copy protocol)}{\ttindex{__copy__()}\ttindex{__deepcopy__()}}
Fred Drake3d299551998-11-30 18:59:44 +000094
95\begin{seealso}
96\seemodule{pickle}{Discussion of the special disciplines used to
97support object state retrieval and restoration.}
98\end{seealso}