Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / pickle-inst.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node69.html" />
<link rel="prev" href="pickle-protocol.html" />
<link rel="parent" href="pickle-protocol.html" />
<link rel="next" href="node69.html" />
<meta name='aesop' content='information' />
<title>3.14.5.1 Pickling and unpickling normal class instances</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.14.5 The pickle protocol"
href="pickle-protocol.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3.14.5 The pickle protocol"
href="pickle-protocol.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.14.5.2 Pickling and unpickling"
href="node69.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="pickle-protocol.html">3.14.5 The pickle protocol</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="pickle-protocol.html">3.14.5 The pickle protocol</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node69.html">3.14.5.2 Pickling and unpickling</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H3><A NAME="SECTION0051451000000000000000"></A><A NAME="pickle-inst"></A>
<BR>
3.14.5.1 Pickling and unpickling normal class
instances
</H3>
<P>
When a pickled class instance is unpickled, its <tt class="method">__init__()</tt>
method is normally <em>not</em> invoked. If it is desirable that the
<tt class="method">__init__()</tt> method be called on unpickling, an old-style class
can define a method <tt class="method">__getinitargs__()</tt>, which should return a
<em>tuple</em> containing the arguments to be passed to the class
constructor (i.e. <tt class="method">__init__()</tt>). The
<tt class="method">__getinitargs__()</tt> method is called at
pickle time; the tuple it returns is incorporated in the pickle for
the instance.
<a id='l2h-648' xml:id='l2h-648'></a><a id='l2h-650' xml:id='l2h-650'></a>
<P>
<a id='l2h-652' xml:id='l2h-652'></a>
<P>
New-style types can provide a <tt class="method">__getnewargs__()</tt> method that is
used for protocol 2. Implementing this method is needed if the type
establishes some internal invariants when the instance is created, or
if the memory allocation is affected by the values passed to the
<tt class="method">__new__()</tt> method for the type (as it is for tuples and
strings). Instances of a new-style type <tt class="class">C</tt> are created using
<P>
<div class="verbatim"><pre><TT>
obj = C.__new__(C, *<var>args</var>)
</TT></pre></div>
<P>
where <var>args</var> is the result of calling <tt class="method">__getnewargs__()</tt> on
the original object; if there is no <tt class="method">__getnewargs__()</tt>, an
empty tuple is assumed.
<P>
<a id='l2h-654' xml:id='l2h-654'></a><a id='l2h-656' xml:id='l2h-656'></a>
<P>
Classes can further influence how their instances are pickled; if the
class defines the method <tt class="method">__getstate__()</tt>, it is called and the
return state is pickled as the contents for the instance, instead of
the contents of the instance's dictionary. If there is no
<tt class="method">__getstate__()</tt> method, the instance's <tt class="member">__dict__</tt> is
pickled.
<P>
Upon unpickling, if the class also defines the method
<tt class="method">__setstate__()</tt>, it is called with the unpickled
state.<A NAME="tex2html18"
HREF="#foot8816"><SUP>3.6</SUP></A> If there is no <tt class="method">__setstate__()</tt> method, the
pickled state must be a dictionary and its items are assigned to the
new instance's dictionary. If a class defines both
<tt class="method">__getstate__()</tt> and <tt class="method">__setstate__()</tt>, the state object
needn't be a dictionary and these methods can do what they
want.<A NAME="tex2html19"
HREF="#foot8979"><SUP>3.7</SUP></A>
<P>
<div class="warning"><b class="label">Warning:</b>
For new-style classes, if <tt class="method">__getstate__()</tt> returns a false
value, the <tt class="method">__setstate__()</tt> method will not be called.
</div>
<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot8816">...
state.</A><A
href="pickle-inst.html#tex2html18"><SUP>3.6</SUP></A></DT>
<DD>These methods can also be used to implement copying
class instances.
</DD>
<DT><A NAME="foot8979">...
want.</A><A
href="pickle-inst.html#tex2html19"><SUP>3.7</SUP></A></DT>
<DD>This protocol is also used by the shallow and deep
copying operations defined in the
<tt class="module"><a href="module-copy.html">copy</a></tt> module.
</DD>
</DL>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.14.5 The pickle protocol"
href="pickle-protocol.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3.14.5 The pickle protocol"
href="pickle-protocol.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.14.5.2 Pickling and unpickling"
href="node69.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="pickle-protocol.html">3.14.5 The pickle protocol</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="pickle-protocol.html">3.14.5 The pickle protocol</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node69.html">3.14.5.2 Pickling and unpickling</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>