Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / module-copy.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="module-marshal.html" />
13<link rel="prev" href="module-shelve.html" />
14<link rel="parent" href="python.html" />
15<link rel="next" href="module-marshal.html" />
16<meta name='aesop' content='information' />
17<title>3.18 copy -- Shallow and deep copy operations</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="3.17.2 Example"
25 href="node77.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
28 href="python.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="3.19 marshal "
31 href="module-marshal.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="node77.html">3.17.2 Example</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="module-marshal.html">3.19 marshal </A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION0051800000000000000000">
563.18 <tt class="module">copy</tt> --
57 Shallow and deep copy operations</A>
58</H1>
59
60<P>
61<A NAME="module-copy"></A>
62
63<P>
64This module provides generic (shallow and deep) copying operations.
65<a id='l2h-675' xml:id='l2h-675'></a>
66<P>
67Interface summary:
68
69<P>
70<div class="verbatim"><pre>
71import copy
72
73x = copy.copy(y) # make a shallow copy of y
74x = copy.deepcopy(y) # make a deep copy of y
75</pre></div>
76For module specific errors, <tt class="exception">copy.error</tt> is raised.
77
78<P>
79The difference between shallow and deep copying is only relevant for
80compound objects (objects that contain other objects, like lists or
81class instances):
82
83<P>
84
85<UL>
86<LI>A <em>shallow copy</em> constructs a new compound object and then (to the
87extent possible) inserts <em>references</em> into it to the objects found
88in the original.
89
90<P>
91</LI>
92<LI>A <em>deep copy</em> constructs a new compound object and then,
93recursively, inserts <em>copies</em> into it of the objects found in the
94original.
95
96<P>
97</LI>
98</UL>
99
100<P>
101Two problems often exist with deep copy operations that don't exist
102with shallow copy operations:
103
104<P>
105
106<UL>
107<LI>Recursive objects (compound objects that, directly or indirectly,
108contain a reference to themselves) may cause a recursive loop.
109
110<P>
111</LI>
112<LI>Because deep copy copies <em>everything</em> it may copy too much,
113e.g., administrative data structures that should be shared even
114between copies.
115
116<P>
117</LI>
118</UL>
119
120<P>
121The <tt class="function">deepcopy()</tt> function avoids these problems by:
122
123<P>
124
125<UL>
126<LI>keeping a ``memo'' dictionary of objects already copied during the current
127copying pass; and
128
129<P>
130</LI>
131<LI>letting user-defined classes override the copying operation or the
132set of components copied.
133
134<P>
135</LI>
136</UL>
137
138<P>
139This version does not copy types like module, class, function, method,
140stack trace, stack frame, file, socket, window, array, or any similar
141types.
142
143<P>
144Classes can use the same interfaces to control copying that they use
145to control pickling. See the description of module
146<tt class="module"><a href="module-pickle.html">pickle</a></tt><a id='l2h-678' xml:id='l2h-678'></a> for information on these
147methods. The <tt class="module">copy</tt> module does not use the
148<tt class="module"><a href="module-copyreg.html">copy_reg</a></tt> registration module.
149
150<P>
151In order for a class to define its own copy implementation, it can
152define special methods <tt class="method">__copy__()</tt> and
153<tt class="method">__deepcopy__()</tt>. The former is called to implement the
154shallow copy operation; no additional arguments are passed. The
155latter is called to implement the deep copy operation; it is passed
156one argument, the memo dictionary. If the <tt class="method">__deepcopy__()</tt>
157implementation needs to make a deep copy of a component, it should
158call the <tt class="function">deepcopy()</tt> function with the component as first
159argument and the memo dictionary as second argument.
160<a id='l2h-677' xml:id='l2h-677'></a>
161<P>
162<div class="seealso">
163 <p class="heading">See Also:</p>
164
165<dl compact="compact" class="seemodule">
166 <dt>Module <b><tt class="module"><a href="module-pickle.html">pickle</a></tt>:</b>
167 <dd>Discussion of the special methods used to
168support object state retrieval and restoration.
169 </dl>
170</div>
171
172<DIV CLASS="navigation">
173<div class='online-navigation'>
174<p></p><hr />
175<table align="center" width="100%" cellpadding="0" cellspacing="2">
176<tr>
177<td class='online-navigation'><a rel="prev" title="3.17.2 Example"
178 href="node77.html"><img src='../icons/previous.png'
179 border='0' height='32' alt='Previous Page' width='32' /></A></td>
180<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
181 href="python.html"><img src='../icons/up.png'
182 border='0' height='32' alt='Up One Level' width='32' /></A></td>
183<td class='online-navigation'><a rel="next" title="3.19 marshal "
184 href="module-marshal.html"><img src='../icons/next.png'
185 border='0' height='32' alt='Next Page' width='32' /></A></td>
186<td align="center" width="100%">Python Library Reference</td>
187<td class='online-navigation'><a rel="contents" title="Table of Contents"
188 href="contents.html"><img src='../icons/contents.png'
189 border='0' height='32' alt='Contents' width='32' /></A></td>
190<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
191 border='0' height='32' alt='Module Index' width='32' /></a></td>
192<td class='online-navigation'><a rel="index" title="Index"
193 href="genindex.html"><img src='../icons/index.png'
194 border='0' height='32' alt='Index' width='32' /></A></td>
195</tr></table>
196<div class='online-navigation'>
197<b class="navlabel">Previous:</b>
198<a class="sectref" rel="prev" href="node77.html">3.17.2 Example</A>
199<b class="navlabel">Up:</b>
200<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
201<b class="navlabel">Next:</b>
202<a class="sectref" rel="next" href="module-marshal.html">3.19 marshal </A>
203</div>
204</div>
205<hr />
206<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
207</DIV>
208<!--End of Navigation Panel-->
209<ADDRESS>
210See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
211</ADDRESS>
212</BODY>
213</HTML>