Quick update to the README file. For intros and books we now point to
[python/dscho.git] / Doc / lib / libcopy.tex
blob091d411bf13356433797961782e282ffe4808cdd
1 \section{\module{copy} ---
2 Shallow and deep copy operations}
4 \declaremodule{standard}{copy}
5 \modulesynopsis{Shallow and deep copy operations.}
8 This module provides generic (shallow and deep) copying operations.
9 \withsubitem{(in copy)}{\ttindex{copy()}\ttindex{deepcopy()}}
11 Interface summary:
13 \begin{verbatim}
14 import copy
16 x = copy.copy(y) # make a shallow copy of y
17 x = copy.deepcopy(y) # make a deep copy of y
18 \end{verbatim}
20 For module specific errors, \exception{copy.error} is raised.
22 The difference between shallow and deep copying is only relevant for
23 compound objects (objects that contain other objects, like lists or
24 class instances):
26 \begin{itemize}
28 \item
29 A \emph{shallow copy} constructs a new compound object and then (to the
30 extent possible) inserts \emph{references} into it to the objects found
31 in the original.
33 \item
34 A \emph{deep copy} constructs a new compound object and then,
35 recursively, inserts \emph{copies} into it of the objects found in the
36 original.
38 \end{itemize}
40 Two problems often exist with deep copy operations that don't exist
41 with shallow copy operations:
43 \begin{itemize}
45 \item
46 Recursive objects (compound objects that, directly or indirectly,
47 contain a reference to themselves) may cause a recursive loop.
49 \item
50 Because deep copy copies \emph{everything} it may copy too much,
51 e.g., administrative data structures that should be shared even
52 between copies.
54 \end{itemize}
56 The \function{deepcopy()} function avoids these problems by:
58 \begin{itemize}
60 \item
61 keeping a ``memo'' dictionary of objects already copied during the current
62 copying pass; and
64 \item
65 letting user-defined classes override the copying operation or the
66 set of components copied.
68 \end{itemize}
70 This version does not copy types like module, class, function, method,
71 stack trace, stack frame, file, socket, window, array, or any similar
72 types.
74 Classes can use the same interfaces to control copying that they use
75 to control pickling: they can define methods called
76 \method{__getinitargs__()}, \method{__getstate__()} and
77 \method{__setstate__()}. See the description of module
78 \refmodule{pickle}\refstmodindex{pickle} for information on these
79 methods. The \module{copy} module does not use the
80 \refmodule[copyreg]{copy_reg} registration module.
81 \withsubitem{(copy protocol)}{\ttindex{__getinitargs__()}
82 \ttindex{__getstate__()}\ttindex{__setstate__()}}
84 In order for a class to define its own copy implementation, it can
85 define special methods \method{__copy__()} and
86 \method{__deepcopy__()}. The former is called to implement the
87 shallow copy operation; no additional arguments are passed. The
88 latter is called to implement the deep copy operation; it is passed
89 one argument, the memo dictionary. If the \method{__deepcopy__()}
90 implementation needs to make a deep copy of a component, it should
91 call the \function{deepcopy()} function with the component as first
92 argument and the memo dictionary as second argument.
93 \withsubitem{(copy protocol)}{\ttindex{__copy__()}\ttindex{__deepcopy__()}}
95 \begin{seealso}
96 \seemodule{pickle}{Discussion of the special disciplines used to
97 support object state retrieval and restoration.}
98 \end{seealso}