source: vendor/python/2.5/Doc/lib/libcopy.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 3.1 KB
Line 
1\section{\module{copy} ---
2 Shallow and deep copy operations}
3
4\declaremodule{standard}{copy}
5\modulesynopsis{Shallow and deep copy operations.}
6
7
8This module provides generic (shallow and deep) copying operations.
9\withsubitem{(in copy)}{\ttindex{copy()}\ttindex{deepcopy()}}
10
11Interface summary:
12
13\begin{verbatim}
14import copy
15
16x = copy.copy(y) # make a shallow copy of y
17x = copy.deepcopy(y) # make a deep copy of y
18\end{verbatim}
19%
20For module specific errors, \exception{copy.error} is raised.
21
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
29A \emph{shallow copy} constructs a new compound object and then (to the
30extent possible) inserts \emph{references} into it to the objects found
31in the original.
32
33\item
34A \emph{deep copy} constructs a new compound object and then,
35recursively, inserts \emph{copies} into it of the objects found in the
36original.
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
50Because deep copy copies \emph{everything} it may copy too much,
51e.g., administrative data structures that should be shared even
52between copies.
53
54\end{itemize}
55
56The \function{deepcopy()} function avoids these problems by:
57
58\begin{itemize}
59
60\item
61keeping a ``memo'' dictionary of objects already copied during the current
62copying 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 module does not copy types like module, method,
71stack trace, stack frame, file, socket, window, array, or any similar
72types. It does ``copy'' functions and classes (shallow and deeply),
73by returning the original object unchanged; this is compatible with
74the way these are treated by the \module{pickle} module.
75\versionchanged[Added copying functions]{2.5}
76
77Classes can use the same interfaces to control copying that they use
78to control pickling. See the description of module
79\refmodule{pickle}\refstmodindex{pickle} for information on these
80methods. The \module{copy} module does not use the
81\refmodule[copyreg]{copy_reg} registration module.
82
83In order for a class to define its own copy implementation, it can
84define special methods \method{__copy__()} and
85\method{__deepcopy__()}. The former is called to implement the
86shallow copy operation; no additional arguments are passed. The
87latter is called to implement the deep copy operation; it is passed
88one argument, the memo dictionary. If the \method{__deepcopy__()}
89implementation needs to make a deep copy of a component, it should
90call the \function{deepcopy()} function with the component as first
91argument and the memo dictionary as second argument.
92\withsubitem{(copy protocol)}{\ttindex{__copy__()}\ttindex{__deepcopy__()}}
93
94\begin{seealso}
95\seemodule{pickle}{Discussion of the special methods used to
96support object state retrieval and restoration.}
97\end{seealso}
Note: See TracBrowser for help on using the repository browser.