blob: 14e667e2547da116ac2769ec0041759b59e7baa5 [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 Drake19479911998-02-13 06:58:54 +00007\setindexsubitem{(copy function)}
Guido van Rossumd1883581995-02-15 15:53:08 +00008\ttindex{copy}
9\ttindex{deepcopy}
10
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%
Guido van Rossum470be141995-03-17 16:07:09 +000022For module specific errors, \code{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 Drakeaf8a0151998-01-14 14:51:31 +000052Because deep copy copies \emph{everything} it may copy too much, e.g.\
Guido van Rossumd1883581995-02-15 15:53:08 +000053administrative data structures that should be shared even between
54copies.
55
56\end{itemize}
57
58Python's \code{deepcopy()} operation avoids these problems by:
59
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
78\code{__getinitargs__()}, \code{__getstate__()} and
79\code{__setstate__()}. See the description of module \code{pickle}
80for information on these methods.
Guido van Rossumcf51dac1998-06-30 16:54:33 +000081The copy module does not use the \module{copy_reg} registration
82module.
Fred Drake54820dc1997-12-15 21:56:05 +000083\refstmodindex{pickle}
Fred Drake19479911998-02-13 06:58:54 +000084\setindexsubitem{(copy protocol)}
Guido van Rossumd1883581995-02-15 15:53:08 +000085\ttindex{__getinitargs__}
86\ttindex{__getstate__}
87\ttindex{__setstate__}
Guido van Rossumcf51dac1998-06-30 16:54:33 +000088
89In order for a class to define its own copy implementation, it can
90define special methods \method{__copy__()}\ttindex{__copy__} and
91\method{__deepcopy__()}\ttindex{__deepcopy__}. The former is called to
92implement the shallow copy operation; no additional arguments are
93passed. The latter is called to implement the deep copy operation; it
94is passed one argument, the memo dictionary. If the
95\method{__deepcopy__()} implementation needs to make a deep copy of a
96component, it should call the \function{deepcopy()} function with the
97component as first argument and the memo dictionary as second
98argument.