Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / pickle-inst.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="node69.html" />
13<link rel="prev" href="pickle-protocol.html" />
14<link rel="parent" href="pickle-protocol.html" />
15<link rel="next" href="node69.html" />
16<meta name='aesop' content='information' />
17<title>3.14.5.1 Pickling and unpickling normal class instances</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.14.5 The pickle protocol"
25 href="pickle-protocol.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.14.5 The pickle protocol"
28 href="pickle-protocol.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.14.5.2 Pickling and unpickling"
31 href="node69.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="pickle-protocol.html">3.14.5 The pickle protocol</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="pickle-protocol.html">3.14.5 The pickle protocol</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="node69.html">3.14.5.2 Pickling and unpickling</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H3><A NAME="SECTION0051451000000000000000"></A><A NAME="pickle-inst"></A>
56<BR>
573.14.5.1 Pickling and unpickling normal class
58 instances
59</H3>
60
61<P>
62When a pickled class instance is unpickled, its <tt class="method">__init__()</tt>
63method is normally <em>not</em> invoked. If it is desirable that the
64<tt class="method">__init__()</tt> method be called on unpickling, an old-style class
65can define a method <tt class="method">__getinitargs__()</tt>, which should return a
66<em>tuple</em> containing the arguments to be passed to the class
67constructor (i.e. <tt class="method">__init__()</tt>). The
68<tt class="method">__getinitargs__()</tt> method is called at
69pickle time; the tuple it returns is incorporated in the pickle for
70the instance.
71<a id='l2h-648' xml:id='l2h-648'></a><a id='l2h-650' xml:id='l2h-650'></a>
72<P>
73<a id='l2h-652' xml:id='l2h-652'></a>
74<P>
75New-style types can provide a <tt class="method">__getnewargs__()</tt> method that is
76used for protocol 2. Implementing this method is needed if the type
77establishes some internal invariants when the instance is created, or
78if the memory allocation is affected by the values passed to the
79<tt class="method">__new__()</tt> method for the type (as it is for tuples and
80strings). Instances of a new-style type <tt class="class">C</tt> are created using
81
82<P>
83<div class="verbatim"><pre><TT>
84 obj = C.__new__(C, *<var>args</var>)
85 </TT></pre></div>
86
87<P>
88where <var>args</var> is the result of calling <tt class="method">__getnewargs__()</tt> on
89the original object; if there is no <tt class="method">__getnewargs__()</tt>, an
90empty tuple is assumed.
91
92<P>
93<a id='l2h-654' xml:id='l2h-654'></a><a id='l2h-656' xml:id='l2h-656'></a>
94<P>
95Classes can further influence how their instances are pickled; if the
96class defines the method <tt class="method">__getstate__()</tt>, it is called and the
97return state is pickled as the contents for the instance, instead of
98the contents of the instance's dictionary. If there is no
99<tt class="method">__getstate__()</tt> method, the instance's <tt class="member">__dict__</tt> is
100pickled.
101
102<P>
103Upon unpickling, if the class also defines the method
104<tt class="method">__setstate__()</tt>, it is called with the unpickled
105state.<A NAME="tex2html18"
106 HREF="#foot8816"><SUP>3.6</SUP></A> If there is no <tt class="method">__setstate__()</tt> method, the
107pickled state must be a dictionary and its items are assigned to the
108new instance's dictionary. If a class defines both
109<tt class="method">__getstate__()</tt> and <tt class="method">__setstate__()</tt>, the state object
110needn't be a dictionary and these methods can do what they
111want.<A NAME="tex2html19"
112 HREF="#foot8979"><SUP>3.7</SUP></A>
113<P>
114<div class="warning"><b class="label">Warning:</b>
115
116 For new-style classes, if <tt class="method">__getstate__()</tt> returns a false
117 value, the <tt class="method">__setstate__()</tt> method will not be called.
118</div>
119
120<P>
121<BR><HR><H4>Footnotes</H4>
122<DL>
123<DT><A NAME="foot8816">...
124state.</A><A
125 href="pickle-inst.html#tex2html18"><SUP>3.6</SUP></A></DT>
126<DD>These methods can also be used to implement copying
127class instances.
128
129</DD>
130<DT><A NAME="foot8979">...
131want.</A><A
132 href="pickle-inst.html#tex2html19"><SUP>3.7</SUP></A></DT>
133<DD>This protocol is also used by the shallow and deep
134copying operations defined in the
135<tt class="module"><a href="module-copy.html">copy</a></tt> module.
136
137</DD>
138</DL>
139<DIV CLASS="navigation">
140<div class='online-navigation'>
141<p></p><hr />
142<table align="center" width="100%" cellpadding="0" cellspacing="2">
143<tr>
144<td class='online-navigation'><a rel="prev" title="3.14.5 The pickle protocol"
145 href="pickle-protocol.html"><img src='../icons/previous.png'
146 border='0' height='32' alt='Previous Page' width='32' /></A></td>
147<td class='online-navigation'><a rel="parent" title="3.14.5 The pickle protocol"
148 href="pickle-protocol.html"><img src='../icons/up.png'
149 border='0' height='32' alt='Up One Level' width='32' /></A></td>
150<td class='online-navigation'><a rel="next" title="3.14.5.2 Pickling and unpickling"
151 href="node69.html"><img src='../icons/next.png'
152 border='0' height='32' alt='Next Page' width='32' /></A></td>
153<td align="center" width="100%">Python Library Reference</td>
154<td class='online-navigation'><a rel="contents" title="Table of Contents"
155 href="contents.html"><img src='../icons/contents.png'
156 border='0' height='32' alt='Contents' width='32' /></A></td>
157<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
158 border='0' height='32' alt='Module Index' width='32' /></a></td>
159<td class='online-navigation'><a rel="index" title="Index"
160 href="genindex.html"><img src='../icons/index.png'
161 border='0' height='32' alt='Index' width='32' /></A></td>
162</tr></table>
163<div class='online-navigation'>
164<b class="navlabel">Previous:</b>
165<a class="sectref" rel="prev" href="pickle-protocol.html">3.14.5 The pickle protocol</A>
166<b class="navlabel">Up:</b>
167<a class="sectref" rel="parent" href="pickle-protocol.html">3.14.5 The pickle protocol</A>
168<b class="navlabel">Next:</b>
169<a class="sectref" rel="next" href="node69.html">3.14.5.2 Pickling and unpickling</A>
170</div>
171</div>
172<hr />
173<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
174</DIV>
175<!--End of Navigation Panel-->
176<ADDRESS>
177See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
178</ADDRESS>
179</BODY>
180</HTML>