blob: c0194768e3359a799a3c8a5e797648e6d66f0129 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{copy}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{copy}
3
4\modulesynopsis{Shallow and deep copy operations.}
5
Fred Drake19479911998-02-13 06:58:54 +00006\setindexsubitem{(copy function)}
Guido van Rossumd1883581995-02-15 15:53:08 +00007\ttindex{copy}
8\ttindex{deepcopy}
9
10This module provides generic (shallow and deep) copying operations.
11
12Interface summary:
13
Fred Drake19479911998-02-13 06:58:54 +000014\begin{verbatim}
Guido van Rossumd1883581995-02-15 15:53:08 +000015import copy
16
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000017x = copy.copy(y) # make a shallow copy of y
18x = copy.deepcopy(y) # make a deep copy of y
Fred Drake19479911998-02-13 06:58:54 +000019\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000020%
Guido van Rossum470be141995-03-17 16:07:09 +000021For module specific errors, \code{copy.error} is raised.
Guido van Rossumd1883581995-02-15 15:53:08 +000022
23The difference between shallow and deep copying is only relevant for
24compound objects (objects that contain other objects, like lists or
25class instances):
26
27\begin{itemize}
28
29\item
Fred Drakeaf8a0151998-01-14 14:51:31 +000030A \emph{shallow copy} constructs a new compound object and then (to the
31extent possible) inserts \emph{references} into it to the objects found
Guido van Rossumd1883581995-02-15 15:53:08 +000032in the original.
33
34\item
Fred Drakeaf8a0151998-01-14 14:51:31 +000035A \emph{deep copy} constructs a new compound object and then,
36recursively, inserts \emph{copies} into it of the objects found in the
Guido van Rossumd1883581995-02-15 15:53:08 +000037original.
38
39\end{itemize}
40
41Two problems often exist with deep copy operations that don't exist
42with shallow copy operations:
43
44\begin{itemize}
45
46\item
47Recursive objects (compound objects that, directly or indirectly,
48contain a reference to themselves) may cause a recursive loop.
49
50\item
Fred Drakeaf8a0151998-01-14 14:51:31 +000051Because deep copy copies \emph{everything} it may copy too much, e.g.\
Guido van Rossumd1883581995-02-15 15:53:08 +000052administrative data structures that should be shared even between
53copies.
54
55\end{itemize}
56
57Python's \code{deepcopy()} operation avoids these problems by:
58
59\begin{itemize}
60
61\item
Guido van Rossumcf51dac1998-06-30 16:54:33 +000062keeping a ``memo'' dictionary of objects already copied during the current
Guido van Rossumd1883581995-02-15 15:53:08 +000063copying pass; and
64
65\item
66letting user-defined classes override the copying operation or the
67set of components copied.
68
69\end{itemize}
70
71This version does not copy types like module, class, function, method,
72nor stack trace, stack frame, nor file, socket, window, nor array, nor
73any similar types.
74
75Classes can use the same interfaces to control copying that they use
76to control pickling: they can define methods called
77\code{__getinitargs__()}, \code{__getstate__()} and
78\code{__setstate__()}. See the description of module \code{pickle}
79for information on these methods.
Guido van Rossumcf51dac1998-06-30 16:54:33 +000080The copy module does not use the \module{copy_reg} registration
81module.
Fred Drake54820dc1997-12-15 21:56:05 +000082\refstmodindex{pickle}
Fred Drake19479911998-02-13 06:58:54 +000083\setindexsubitem{(copy protocol)}
Guido van Rossumd1883581995-02-15 15:53:08 +000084\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
89define special methods \method{__copy__()}\ttindex{__copy__} and
90\method{__deepcopy__()}\ttindex{__deepcopy__}. The former is called to
91implement the shallow copy operation; no additional arguments are
92passed. The latter is called to implement the deep copy operation; it
93is passed one argument, the memo dictionary. If the
94\method{__deepcopy__()} implementation needs to make a deep copy of a
95component, it should call the \function{deepcopy()} function with the
96component as first argument and the memo dictionary as second
97argument.