1 | \section{\module{copy} ---
|
---|
2 | Shallow and deep copy operations}
|
---|
3 |
|
---|
4 | \declaremodule{standard}{copy}
|
---|
5 | \modulesynopsis{Shallow and deep copy operations.}
|
---|
6 |
|
---|
7 |
|
---|
8 | This module provides generic (shallow and deep) copying operations.
|
---|
9 | \withsubitem{(in copy)}{\ttindex{copy()}\ttindex{deepcopy()}}
|
---|
10 |
|
---|
11 | Interface summary:
|
---|
12 |
|
---|
13 | \begin{verbatim}
|
---|
14 | import copy
|
---|
15 |
|
---|
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}
|
---|
19 | %
|
---|
20 | For module specific errors, \exception{copy.error} is raised.
|
---|
21 |
|
---|
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):
|
---|
25 |
|
---|
26 | \begin{itemize}
|
---|
27 |
|
---|
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.
|
---|
32 |
|
---|
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.
|
---|
37 |
|
---|
38 | \end{itemize}
|
---|
39 |
|
---|
40 | Two problems often exist with deep copy operations that don't exist
|
---|
41 | with shallow copy operations:
|
---|
42 |
|
---|
43 | \begin{itemize}
|
---|
44 |
|
---|
45 | \item
|
---|
46 | Recursive objects (compound objects that, directly or indirectly,
|
---|
47 | contain a reference to themselves) may cause a recursive loop.
|
---|
48 |
|
---|
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.
|
---|
53 |
|
---|
54 | \end{itemize}
|
---|
55 |
|
---|
56 | The \function{deepcopy()} function avoids these problems by:
|
---|
57 |
|
---|
58 | \begin{itemize}
|
---|
59 |
|
---|
60 | \item
|
---|
61 | keeping a ``memo'' dictionary of objects already copied during the current
|
---|
62 | copying pass; and
|
---|
63 |
|
---|
64 | \item
|
---|
65 | letting user-defined classes override the copying operation or the
|
---|
66 | set of components copied.
|
---|
67 |
|
---|
68 | \end{itemize}
|
---|
69 |
|
---|
70 | This module does not copy types like module, method,
|
---|
71 | stack trace, stack frame, file, socket, window, array, or any similar
|
---|
72 | types. It does ``copy'' functions and classes (shallow and deeply),
|
---|
73 | by returning the original object unchanged; this is compatible with
|
---|
74 | the way these are treated by the \module{pickle} module.
|
---|
75 | \versionchanged[Added copying functions]{2.5}
|
---|
76 |
|
---|
77 | Classes can use the same interfaces to control copying that they use
|
---|
78 | to control pickling. See the description of module
|
---|
79 | \refmodule{pickle}\refstmodindex{pickle} for information on these
|
---|
80 | methods. The \module{copy} module does not use the
|
---|
81 | \refmodule[copyreg]{copy_reg} registration module.
|
---|
82 |
|
---|
83 | In order for a class to define its own copy implementation, it can
|
---|
84 | define special methods \method{__copy__()} and
|
---|
85 | \method{__deepcopy__()}. The former is called to implement the
|
---|
86 | shallow copy operation; no additional arguments are passed. The
|
---|
87 | latter is called to implement the deep copy operation; it is passed
|
---|
88 | one argument, the memo dictionary. If the \method{__deepcopy__()}
|
---|
89 | implementation needs to make a deep copy of a component, it should
|
---|
90 | call the \function{deepcopy()} function with the component as first
|
---|
91 | argument 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
|
---|
96 | support object state retrieval and restoration.}
|
---|
97 | \end{seealso}
|
---|