blob: d73d6fdecf9c6473175c2cc58b6a3e17bd8f7f3d [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
Fred Drakef5213c21999-06-29 16:02:12 +00004\declaremodule{standard}{copy}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\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,
Fred Drakef5213c21999-06-29 16:02:12 +000071stack trace, stack frame, file, socket, window, array, or any similar
72types.
Guido van Rossumd1883581995-02-15 15:53:08 +000073
74Classes can use the same interfaces to control copying that they use
Fred Drake04d92c32004-05-05 04:24:30 +000075to control pickling. See the description of module
Fred Drake2d2b6a01999-02-12 20:40:49 +000076\refmodule{pickle}\refstmodindex{pickle} for information on these
Fred Drakef5213c21999-06-29 16:02:12 +000077methods. The \module{copy} module does not use the
78\refmodule[copyreg]{copy_reg} registration module.
Guido van Rossumcf51dac1998-06-30 16:54:33 +000079
80In order for a class to define its own copy implementation, it can
Fred Drake3d299551998-11-30 18:59:44 +000081define special methods \method{__copy__()} and
82\method{__deepcopy__()}. The former is called to implement the
83shallow copy operation; no additional arguments are passed. The
84latter is called to implement the deep copy operation; it is passed
85one argument, the memo dictionary. If the \method{__deepcopy__()}
86implementation needs to make a deep copy of a component, it should
87call the \function{deepcopy()} function with the component as first
88argument and the memo dictionary as second argument.
Fred Drake2d2b6a01999-02-12 20:40:49 +000089\withsubitem{(copy protocol)}{\ttindex{__copy__()}\ttindex{__deepcopy__()}}
Fred Drake3d299551998-11-30 18:59:44 +000090
91\begin{seealso}
Fred Drake51974442000-10-06 20:16:50 +000092\seemodule{pickle}{Discussion of the special methods used to
Fred Drake3d299551998-11-30 18:59:44 +000093support object state retrieval and restoration.}
94\end{seealso}