Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8f1462e52e1797a02c97073eed0b7f92 > files > 837

python-docs-2.6.5-2.5mdv2010.2.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>12.1. pickle — Python object serialization &mdash; Python v2.6.5 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.5',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.5 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.5 documentation" href="../index.html" />
    <link rel="up" title="12. Data Persistence" href="persistence.html" />
    <link rel="next" title="12.3. copy_reg — Register pickle support functions" href="copy_reg.html" />
    <link rel="prev" title="12. Data Persistence" href="persistence.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="copy_reg.html" title="12.3. copy_reg — Register pickle support functions"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="persistence.html" title="12. Data Persistence"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="persistence.html" accesskey="U">12. Data Persistence</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-pickle">
<h1>12.1. <tt class="xref docutils literal"><span class="pre">pickle</span></tt> &#8212; Python object serialization<a class="headerlink" href="#module-pickle" title="Permalink to this headline">¶</a></h1>
<span class="target" id="index-491"></span><p>The <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module implements a fundamental, but powerful algorithm for
serializing and de-serializing a Python object structure.  &#8220;Pickling&#8221; is the
process whereby a Python object hierarchy is converted into a byte stream, and
&#8220;unpickling&#8221; is the inverse operation, whereby a byte stream is converted back
into an object hierarchy.  Pickling (and unpickling) is alternatively known as
&#8220;serialization&#8221;, &#8220;marshalling,&#8221; <a class="footnote-reference" href="#id11" id="id1">[1]</a> or &#8220;flattening&#8221;, however, to avoid
confusion, the terms used here are &#8220;pickling&#8221; and &#8220;unpickling&#8221;.</p>
<p>This documentation describes both the <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module and the
<tt class="xref docutils literal"><span class="pre">cPickle</span></tt> module.</p>
<div class="section" id="relationship-to-other-python-modules">
<h2>12.1.1. Relationship to other Python modules<a class="headerlink" href="#relationship-to-other-python-modules" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module has an optimized cousin called the <tt class="xref docutils literal"><span class="pre">cPickle</span></tt>
module.  As its name implies, <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> is written in C, so it can be up to
1000 times faster than <tt class="xref docutils literal"><span class="pre">pickle</span></tt>.  However it does not support subclassing
of the <a title="pickle.Pickler" class="reference internal" href="#pickle.Pickler"><tt class="xref docutils literal"><span class="pre">Pickler()</span></tt></a> and <a title="pickle.Unpickler" class="reference internal" href="#pickle.Unpickler"><tt class="xref docutils literal"><span class="pre">Unpickler()</span></tt></a> classes, because in <tt class="xref docutils literal"><span class="pre">cPickle</span></tt>
these are functions, not classes.  Most applications have no need for this
functionality, and can benefit from the improved performance of <tt class="xref docutils literal"><span class="pre">cPickle</span></tt>.
Other than that, the interfaces of the two modules are nearly identical; the
common interface is described in this manual and differences are pointed out
where necessary.  In the following discussions, we use the term &#8220;pickle&#8221; to
collectively describe the <tt class="xref docutils literal"><span class="pre">pickle</span></tt> and <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> modules.</p>
<p>The data streams the two modules produce are guaranteed to be interchangeable.</p>
<p>Python has a more primitive serialization module called <a title="Convert Python objects to streams of bytes and back (with different constraints)." class="reference external" href="marshal.html#module-marshal"><tt class="xref docutils literal"><span class="pre">marshal</span></tt></a>, but in
general <tt class="xref docutils literal"><span class="pre">pickle</span></tt> should always be the preferred way to serialize Python
objects.  <a title="Convert Python objects to streams of bytes and back (with different constraints)." class="reference external" href="marshal.html#module-marshal"><tt class="xref docutils literal"><span class="pre">marshal</span></tt></a> exists primarily to support Python&#8217;s <tt class="docutils literal"><span class="pre">.pyc</span></tt>
files.</p>
<p>The <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module differs from <a title="Convert Python objects to streams of bytes and back (with different constraints)." class="reference external" href="marshal.html#module-marshal"><tt class="xref docutils literal"><span class="pre">marshal</span></tt></a> several significant ways:</p>
<ul>
<li><p class="first">The <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module keeps track of the objects it has already serialized,
so that later references to the same object won&#8217;t be serialized again.
<a title="Convert Python objects to streams of bytes and back (with different constraints)." class="reference external" href="marshal.html#module-marshal"><tt class="xref docutils literal"><span class="pre">marshal</span></tt></a> doesn&#8217;t do this.</p>
<p>This has implications both for recursive objects and object sharing.  Recursive
objects are objects that contain references to themselves.  These are not
handled by marshal, and in fact, attempting to marshal recursive objects will
crash your Python interpreter.  Object sharing happens when there are multiple
references to the same object in different places in the object hierarchy being
serialized.  <tt class="xref docutils literal"><span class="pre">pickle</span></tt> stores such objects only once, and ensures that all
other references point to the master copy.  Shared objects remain shared, which
can be very important for mutable objects.</p>
</li>
<li><p class="first"><a title="Convert Python objects to streams of bytes and back (with different constraints)." class="reference external" href="marshal.html#module-marshal"><tt class="xref docutils literal"><span class="pre">marshal</span></tt></a> cannot be used to serialize user-defined classes and their
instances.  <tt class="xref docutils literal"><span class="pre">pickle</span></tt> can save and restore class instances transparently,
however the class definition must be importable and live in the same module as
when the object was stored.</p>
</li>
<li><p class="first">The <a title="Convert Python objects to streams of bytes and back (with different constraints)." class="reference external" href="marshal.html#module-marshal"><tt class="xref docutils literal"><span class="pre">marshal</span></tt></a> serialization format is not guaranteed to be portable
across Python versions.  Because its primary job in life is to support
<tt class="docutils literal"><span class="pre">.pyc</span></tt> files, the Python implementers reserve the right to change the
serialization format in non-backwards compatible ways should the need arise.
The <tt class="xref docutils literal"><span class="pre">pickle</span></tt> serialization format is guaranteed to be backwards compatible
across Python releases.</p>
</li>
</ul>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">The <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module is not intended to be secure against erroneous or
maliciously constructed data.  Never unpickle data received from an untrusted
or unauthenticated source.</p>
</div>
<p>Note that serialization is a more primitive notion than persistence; although
<tt class="xref docutils literal"><span class="pre">pickle</span></tt> reads and writes file objects, it does not handle the issue of
naming persistent objects, nor the (even more complicated) issue of concurrent
access to persistent objects.  The <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module can transform a complex
object into a byte stream and it can transform the byte stream into an object
with the same internal structure.  Perhaps the most obvious thing to do with
these byte streams is to write them onto a file, but it is also conceivable to
send them across a network or store them in a database.  The module
<a title="Python object persistence." class="reference external" href="shelve.html#module-shelve"><tt class="xref docutils literal"><span class="pre">shelve</span></tt></a> provides a simple interface to pickle and unpickle objects on
DBM-style database files.</p>
</div>
<div class="section" id="data-stream-format">
<h2>12.1.2. Data stream format<a class="headerlink" href="#data-stream-format" title="Permalink to this headline">¶</a></h2>
<p id="index-492">The data format used by <tt class="xref docutils literal"><span class="pre">pickle</span></tt> is Python-specific.  This has the
advantage that there are no restrictions imposed by external standards such as
XDR (which can&#8217;t represent pointer sharing); however it means that non-Python
programs may not be able to reconstruct pickled Python objects.</p>
<p>By default, the <tt class="xref docutils literal"><span class="pre">pickle</span></tt> data format uses a printable ASCII representation.
This is slightly more voluminous than a binary representation.  The big
advantage of using printable ASCII (and of some other characteristics of
<tt class="xref docutils literal"><span class="pre">pickle</span></tt>&#8216;s representation) is that for debugging or recovery purposes it is
possible for a human to read the pickled file with a standard text editor.</p>
<p>There are currently 3 different protocols which can be used for pickling.</p>
<ul class="simple">
<li>Protocol version 0 is the original ASCII protocol and is backwards compatible
with earlier versions of Python.</li>
<li>Protocol version 1 is the old binary format which is also compatible with
earlier versions of Python.</li>
<li>Protocol version 2 was introduced in Python 2.3.  It provides much more
efficient pickling of <a class="reference external" href="../glossary.html#term-new-style-class"><em class="xref">new-style class</em></a>es.</li>
</ul>
<p>Refer to <span class="target" id="index-493"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0307"><strong>PEP 307</strong></a> for more information.</p>
<p>If a <em>protocol</em> is not specified, protocol 0 is used. If <em>protocol</em> is specified
as a negative value or <a title="pickle.HIGHEST_PROTOCOL" class="reference internal" href="#pickle.HIGHEST_PROTOCOL"><tt class="xref docutils literal"><span class="pre">HIGHEST_PROTOCOL</span></tt></a>, the highest protocol version
available will be used.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.3: </span>Introduced the <em>protocol</em> parameter.</p>
<p>A binary format, which is slightly more efficient, can be chosen by specifying a
<em>protocol</em> version &gt;= 1.</p>
</div>
<div class="section" id="usage">
<h2>12.1.3. Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
<p>To serialize an object hierarchy, you first create a pickler, then you call the
pickler&#8217;s <a title="pickle.dump" class="reference internal" href="#pickle.dump"><tt class="xref docutils literal"><span class="pre">dump()</span></tt></a> method.  To de-serialize a data stream, you first create
an unpickler, then you call the unpickler&#8217;s <a title="pickle.load" class="reference internal" href="#pickle.load"><tt class="xref docutils literal"><span class="pre">load()</span></tt></a> method.  The
<tt class="xref docutils literal"><span class="pre">pickle</span></tt> module provides the following constant:</p>
<dl class="data">
<dt id="pickle.HIGHEST_PROTOCOL">
<tt class="descclassname">pickle.</tt><tt class="descname">HIGHEST_PROTOCOL</tt><a class="headerlink" href="#pickle.HIGHEST_PROTOCOL" title="Permalink to this definition">¶</a></dt>
<dd><p>The highest protocol version available.  This value can be passed as a
<em>protocol</em> value.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.3.</span></p>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Be sure to always open pickle files created with protocols &gt;= 1 in binary mode.
For the old ASCII-based pickle protocol 0 you can use either text mode or binary
mode as long as you stay consistent.</p>
<p class="last">A pickle file written with protocol 0 in binary mode will contain lone linefeeds
as line terminators and therefore will look &#8220;funny&#8221; when viewed in Notepad or
other editors which do not support this format.</p>
</div>
<p>The <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module provides the following functions to make the pickling
process more convenient:</p>
<dl class="function">
<dt id="pickle.dump">
<tt class="descclassname">pickle.</tt><tt class="descname">dump</tt><big>(</big><em>obj</em>, <em>file</em><span class="optional">[</span>, <em>protocol</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#pickle.dump" title="Permalink to this definition">¶</a></dt>
<dd><p>Write a pickled representation of <em>obj</em> to the open file object <em>file</em>.  This is
equivalent to <tt class="docutils literal"><span class="pre">Pickler(file,</span> <span class="pre">protocol).dump(obj)</span></tt>.</p>
<p>If the <em>protocol</em> parameter is omitted, protocol 0 is used. If <em>protocol</em> is
specified as a negative value or <a title="pickle.HIGHEST_PROTOCOL" class="reference internal" href="#pickle.HIGHEST_PROTOCOL"><tt class="xref docutils literal"><span class="pre">HIGHEST_PROTOCOL</span></tt></a>, the highest protocol
version will be used.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.3: </span>Introduced the <em>protocol</em> parameter.</p>
<p><em>file</em> must have a <tt class="xref docutils literal"><span class="pre">write()</span></tt> method that accepts a single string argument.
It can thus be a file object opened for writing, a <a title="Read and write strings as if they were files." class="reference external" href="stringio.html#module-StringIO"><tt class="xref docutils literal"><span class="pre">StringIO</span></tt></a> object, or
any other custom object that meets this interface.</p>
</dd></dl>

<dl class="function">
<dt id="pickle.load">
<tt class="descclassname">pickle.</tt><tt class="descname">load</tt><big>(</big><em>file</em><big>)</big><a class="headerlink" href="#pickle.load" title="Permalink to this definition">¶</a></dt>
<dd><p>Read a string from the open file object <em>file</em> and interpret it as a pickle data
stream, reconstructing and returning the original object hierarchy.  This is
equivalent to <tt class="docutils literal"><span class="pre">Unpickler(file).load()</span></tt>.</p>
<p><em>file</em> must have two methods, a <tt class="xref docutils literal"><span class="pre">read()</span></tt> method that takes an integer
argument, and a <tt class="xref docutils literal"><span class="pre">readline()</span></tt> method that requires no arguments.  Both
methods should return a string.  Thus <em>file</em> can be a file object opened for
reading, a <a title="Read and write strings as if they were files." class="reference external" href="stringio.html#module-StringIO"><tt class="xref docutils literal"><span class="pre">StringIO</span></tt></a> object, or any other custom object that meets this
interface.</p>
<p>This function automatically determines whether the data stream was written in
binary mode or not.</p>
</dd></dl>

<dl class="function">
<dt id="pickle.dumps">
<tt class="descclassname">pickle.</tt><tt class="descname">dumps</tt><big>(</big><em>obj</em><span class="optional">[</span>, <em>protocol</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#pickle.dumps" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the pickled representation of the object as a string, instead of writing
it to a file.</p>
<p>If the <em>protocol</em> parameter is omitted, protocol 0 is used. If <em>protocol</em> is
specified as a negative value or <a title="pickle.HIGHEST_PROTOCOL" class="reference internal" href="#pickle.HIGHEST_PROTOCOL"><tt class="xref docutils literal"><span class="pre">HIGHEST_PROTOCOL</span></tt></a>, the highest protocol
version will be used.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.3: </span>The <em>protocol</em> parameter was added.</p>
</dd></dl>

<dl class="function">
<dt id="pickle.loads">
<tt class="descclassname">pickle.</tt><tt class="descname">loads</tt><big>(</big><em>string</em><big>)</big><a class="headerlink" href="#pickle.loads" title="Permalink to this definition">¶</a></dt>
<dd>Read a pickled object hierarchy from a string.  Characters in the string past
the pickled object&#8217;s representation are ignored.</dd></dl>

<p>The <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module also defines three exceptions:</p>
<dl class="exception">
<dt id="pickle.PickleError">
<em class="property">exception </em><tt class="descclassname">pickle.</tt><tt class="descname">PickleError</tt><a class="headerlink" href="#pickle.PickleError" title="Permalink to this definition">¶</a></dt>
<dd>A common base class for the other exceptions defined below.  This inherits from
<a title="exceptions.Exception" class="reference external" href="exceptions.html#exceptions.Exception"><tt class="xref docutils literal"><span class="pre">Exception</span></tt></a>.</dd></dl>

<dl class="exception">
<dt id="pickle.PicklingError">
<em class="property">exception </em><tt class="descclassname">pickle.</tt><tt class="descname">PicklingError</tt><a class="headerlink" href="#pickle.PicklingError" title="Permalink to this definition">¶</a></dt>
<dd>This exception is raised when an unpicklable object is passed to the
<a title="pickle.dump" class="reference internal" href="#pickle.dump"><tt class="xref docutils literal"><span class="pre">dump()</span></tt></a> method.</dd></dl>

<dl class="exception">
<dt id="pickle.UnpicklingError">
<em class="property">exception </em><tt class="descclassname">pickle.</tt><tt class="descname">UnpicklingError</tt><a class="headerlink" href="#pickle.UnpicklingError" title="Permalink to this definition">¶</a></dt>
<dd>This exception is raised when there is a problem unpickling an object. Note that
other exceptions may also be raised during unpickling, including (but not
necessarily limited to) <a title="exceptions.AttributeError" class="reference external" href="exceptions.html#exceptions.AttributeError"><tt class="xref docutils literal"><span class="pre">AttributeError</span></tt></a>, <a title="exceptions.EOFError" class="reference external" href="exceptions.html#exceptions.EOFError"><tt class="xref docutils literal"><span class="pre">EOFError</span></tt></a>,
<a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a>, and <a title="exceptions.IndexError" class="reference external" href="exceptions.html#exceptions.IndexError"><tt class="xref docutils literal"><span class="pre">IndexError</span></tt></a>.</dd></dl>

<p>The <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module also exports two callables <a class="footnote-reference" href="#id12" id="id2">[2]</a>, <a title="pickle.Pickler" class="reference internal" href="#pickle.Pickler"><tt class="xref docutils literal"><span class="pre">Pickler</span></tt></a> and
<a title="pickle.Unpickler" class="reference internal" href="#pickle.Unpickler"><tt class="xref docutils literal"><span class="pre">Unpickler</span></tt></a>:</p>
<dl class="class">
<dt id="pickle.Pickler">
<em class="property">class </em><tt class="descclassname">pickle.</tt><tt class="descname">Pickler</tt><big>(</big><em>file</em><span class="optional">[</span>, <em>protocol</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#pickle.Pickler" title="Permalink to this definition">¶</a></dt>
<dd><p>This takes a file-like object to which it will write a pickle data stream.</p>
<p>If the <em>protocol</em> parameter is omitted, protocol 0 is used. If <em>protocol</em> is
specified as a negative value or <a title="pickle.HIGHEST_PROTOCOL" class="reference internal" href="#pickle.HIGHEST_PROTOCOL"><tt class="xref docutils literal"><span class="pre">HIGHEST_PROTOCOL</span></tt></a>, the highest
protocol version will be used.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.3: </span>Introduced the <em>protocol</em> parameter.</p>
<p><em>file</em> must have a <tt class="xref docutils literal"><span class="pre">write()</span></tt> method that accepts a single string argument.
It can thus be an open file object, a <a title="Read and write strings as if they were files." class="reference external" href="stringio.html#module-StringIO"><tt class="xref docutils literal"><span class="pre">StringIO</span></tt></a> object, or any other
custom object that meets this interface.</p>
<p><a title="pickle.Pickler" class="reference internal" href="#pickle.Pickler"><tt class="xref docutils literal"><span class="pre">Pickler</span></tt></a> objects define one (or two) public methods:</p>
<dl class="method">
<dt id="pickle.Pickler.dump">
<tt class="descname">dump</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#pickle.Pickler.dump" title="Permalink to this definition">¶</a></dt>
<dd>Write a pickled representation of <em>obj</em> to the open file object given in the
constructor.  Either the binary or ASCII format will be used, depending on the
value of the <em>protocol</em> argument passed to the constructor.</dd></dl>

<dl class="method">
<dt id="pickle.Pickler.clear_memo">
<tt class="descname">clear_memo</tt><big>(</big><big>)</big><a class="headerlink" href="#pickle.Pickler.clear_memo" title="Permalink to this definition">¶</a></dt>
<dd><p>Clears the pickler&#8217;s &#8220;memo&#8221;.  The memo is the data structure that remembers
which objects the pickler has already seen, so that shared or recursive objects
pickled by reference and not by value.  This method is useful when re-using
picklers.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Prior to Python 2.3, <a title="pickle.Pickler.clear_memo" class="reference internal" href="#pickle.Pickler.clear_memo"><tt class="xref docutils literal"><span class="pre">clear_memo()</span></tt></a> was only available on the picklers
created by <tt class="xref docutils literal"><span class="pre">cPickle</span></tt>.  In the <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module, picklers have an
instance variable called <tt class="xref docutils literal"><span class="pre">memo</span></tt> which is a Python dictionary.  So to clear
the memo for a <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module pickler, you could do the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">mypickler</span><span class="o">.</span><span class="n">memo</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
</pre></div>
</div>
<p class="last">Code that does not need to support older versions of Python should simply use
<a title="pickle.Pickler.clear_memo" class="reference internal" href="#pickle.Pickler.clear_memo"><tt class="xref docutils literal"><span class="pre">clear_memo()</span></tt></a>.</p>
</div>
</dd></dl>

</dd></dl>

<p>It is possible to make multiple calls to the <a title="pickle.dump" class="reference internal" href="#pickle.dump"><tt class="xref docutils literal"><span class="pre">dump()</span></tt></a> method of the same
<a title="pickle.Pickler" class="reference internal" href="#pickle.Pickler"><tt class="xref docutils literal"><span class="pre">Pickler</span></tt></a> instance.  These must then be matched to the same number of
calls to the <a title="pickle.load" class="reference internal" href="#pickle.load"><tt class="xref docutils literal"><span class="pre">load()</span></tt></a> method of the corresponding <a title="pickle.Unpickler" class="reference internal" href="#pickle.Unpickler"><tt class="xref docutils literal"><span class="pre">Unpickler</span></tt></a>
instance.  If the same object is pickled by multiple <a title="pickle.dump" class="reference internal" href="#pickle.dump"><tt class="xref docutils literal"><span class="pre">dump()</span></tt></a> calls, the
<a title="pickle.load" class="reference internal" href="#pickle.load"><tt class="xref docutils literal"><span class="pre">load()</span></tt></a> will all yield references to the same object. <a class="footnote-reference" href="#id13" id="id3">[3]</a></p>
<p><a title="pickle.Unpickler" class="reference internal" href="#pickle.Unpickler"><tt class="xref docutils literal"><span class="pre">Unpickler</span></tt></a> objects are defined as:</p>
<dl class="class">
<dt id="pickle.Unpickler">
<em class="property">class </em><tt class="descclassname">pickle.</tt><tt class="descname">Unpickler</tt><big>(</big><em>file</em><big>)</big><a class="headerlink" href="#pickle.Unpickler" title="Permalink to this definition">¶</a></dt>
<dd><p>This takes a file-like object from which it will read a pickle data stream.
This class automatically determines whether the data stream was written in
binary mode or not, so it does not need a flag as in the <a title="pickle.Pickler" class="reference internal" href="#pickle.Pickler"><tt class="xref docutils literal"><span class="pre">Pickler</span></tt></a>
factory.</p>
<p><em>file</em> must have two methods, a <tt class="xref docutils literal"><span class="pre">read()</span></tt> method that takes an integer
argument, and a <tt class="xref docutils literal"><span class="pre">readline()</span></tt> method that requires no arguments.  Both
methods should return a string.  Thus <em>file</em> can be a file object opened for
reading, a <a title="Read and write strings as if they were files." class="reference external" href="stringio.html#module-StringIO"><tt class="xref docutils literal"><span class="pre">StringIO</span></tt></a> object, or any other custom object that meets this
interface.</p>
<p><a title="pickle.Unpickler" class="reference internal" href="#pickle.Unpickler"><tt class="xref docutils literal"><span class="pre">Unpickler</span></tt></a> objects have one (or two) public methods:</p>
<dl class="method">
<dt id="pickle.Unpickler.load">
<tt class="descname">load</tt><big>(</big><big>)</big><a class="headerlink" href="#pickle.Unpickler.load" title="Permalink to this definition">¶</a></dt>
<dd><p>Read a pickled object representation from the open file object given in
the constructor, and return the reconstituted object hierarchy specified
therein.</p>
<p>This method automatically determines whether the data stream was written
in binary mode or not.</p>
</dd></dl>

<dl class="method">
<dt id="pickle.Unpickler.noload">
<tt class="descname">noload</tt><big>(</big><big>)</big><a class="headerlink" href="#pickle.Unpickler.noload" title="Permalink to this definition">¶</a></dt>
<dd><p>This is just like <a title="pickle.load" class="reference internal" href="#pickle.load"><tt class="xref docutils literal"><span class="pre">load()</span></tt></a> except that it doesn&#8217;t actually create any
objects.  This is useful primarily for finding what&#8217;s called &#8220;persistent
ids&#8221; that may be referenced in a pickle data stream.  See section
<a class="reference internal" href="#pickle-protocol"><em>The pickle protocol</em></a> below for more details.</p>
<p><strong>Note:</strong> the <a title="pickle.Unpickler.noload" class="reference internal" href="#pickle.Unpickler.noload"><tt class="xref docutils literal"><span class="pre">noload()</span></tt></a> method is currently only available on
<a title="pickle.Unpickler" class="reference internal" href="#pickle.Unpickler"><tt class="xref docutils literal"><span class="pre">Unpickler</span></tt></a> objects created with the <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> module.
<tt class="xref docutils literal"><span class="pre">pickle</span></tt> module <a title="pickle.Unpickler" class="reference internal" href="#pickle.Unpickler"><tt class="xref docutils literal"><span class="pre">Unpickler</span></tt></a>s do not have the <a title="pickle.Unpickler.noload" class="reference internal" href="#pickle.Unpickler.noload"><tt class="xref docutils literal"><span class="pre">noload()</span></tt></a>
method.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="what-can-be-pickled-and-unpickled">
<h2>12.1.4. What can be pickled and unpickled?<a class="headerlink" href="#what-can-be-pickled-and-unpickled" title="Permalink to this headline">¶</a></h2>
<p>The following types can be pickled:</p>
<ul class="simple">
<li><tt class="xref docutils literal"><span class="pre">None</span></tt>, <tt class="xref docutils literal"><span class="pre">True</span></tt>, and <tt class="xref docutils literal"><span class="pre">False</span></tt></li>
<li>integers, long integers, floating point numbers, complex numbers</li>
<li>normal and Unicode strings</li>
<li>tuples, lists, sets, and dictionaries containing only picklable objects</li>
<li>functions defined at the top level of a module</li>
<li>built-in functions defined at the top level of a module</li>
<li>classes that are defined at the top level of a module</li>
<li>instances of such classes whose <tt class="xref docutils literal"><span class="pre">__dict__</span></tt> or <a title="object.__setstate__" class="reference internal" href="#object.__setstate__"><tt class="xref docutils literal"><span class="pre">__setstate__()</span></tt></a> is
picklable  (see section <a class="reference internal" href="#pickle-protocol"><em>The pickle protocol</em></a> for details)</li>
</ul>
<p>Attempts to pickle unpicklable objects will raise the <a title="pickle.PicklingError" class="reference internal" href="#pickle.PicklingError"><tt class="xref docutils literal"><span class="pre">PicklingError</span></tt></a>
exception; when this happens, an unspecified number of bytes may have already
been written to the underlying file. Trying to pickle a highly recursive data
structure may exceed the maximum recursion depth, a <a title="exceptions.RuntimeError" class="reference external" href="exceptions.html#exceptions.RuntimeError"><tt class="xref docutils literal"><span class="pre">RuntimeError</span></tt></a> will be
raised in this case. You can carefully raise this limit with
<a title="sys.setrecursionlimit" class="reference external" href="sys.html#sys.setrecursionlimit"><tt class="xref docutils literal"><span class="pre">sys.setrecursionlimit()</span></tt></a>.</p>
<p>Note that functions (built-in and user-defined) are pickled by &#8220;fully qualified&#8221;
name reference, not by value.  This means that only the function name is
pickled, along with the name of module the function is defined in.  Neither the
function&#8217;s code, nor any of its function attributes are pickled.  Thus the
defining module must be importable in the unpickling environment, and the module
must contain the named object, otherwise an exception will be raised. <a class="footnote-reference" href="#id14" id="id4">[4]</a></p>
<p>Similarly, classes are pickled by named reference, so the same restrictions in
the unpickling environment apply.  Note that none of the class&#8217;s code or data is
pickled, so in the following example the class attribute <tt class="docutils literal"><span class="pre">attr</span></tt> is not
restored in the unpickling environment:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span>
    <span class="n">attr</span> <span class="o">=</span> <span class="s">&#39;a class attr&#39;</span>

<span class="n">picklestring</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">Foo</span><span class="p">)</span>
</pre></div>
</div>
<p>These restrictions are why picklable functions and classes must be defined in
the top level of a module.</p>
<p>Similarly, when class instances are pickled, their class&#8217;s code and data are not
pickled along with them.  Only the instance data are pickled.  This is done on
purpose, so you can fix bugs in a class or add methods to the class and still
load objects that were created with an earlier version of the class.  If you
plan to have long-lived objects that will see many versions of a class, it may
be worthwhile to put a version number in the objects so that suitable
conversions can be made by the class&#8217;s <a title="object.__setstate__" class="reference internal" href="#object.__setstate__"><tt class="xref docutils literal"><span class="pre">__setstate__()</span></tt></a> method.</p>
</div>
<div class="section" id="the-pickle-protocol">
<span id="pickle-protocol"></span><h2>12.1.5. The pickle protocol<a class="headerlink" href="#the-pickle-protocol" title="Permalink to this headline">¶</a></h2>
<p>This section describes the &#8220;pickling protocol&#8221; that defines the interface
between the pickler/unpickler and the objects that are being serialized.  This
protocol provides a standard way for you to define, customize, and control how
your objects are serialized and de-serialized.  The description in this section
doesn&#8217;t cover specific customizations that you can employ to make the unpickling
environment slightly safer from untrusted pickle data streams; see section
<a class="reference internal" href="#pickle-sub"><em>Subclassing Unpicklers</em></a> for more details.</p>
<div class="section" id="pickling-and-unpickling-normal-class-instances">
<span id="pickle-inst"></span><h3>12.1.5.1. Pickling and unpickling normal class instances<a class="headerlink" href="#pickling-and-unpickling-normal-class-instances" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="object.__getinitargs__">
<tt class="descclassname">object.</tt><tt class="descname">__getinitargs__</tt><big>(</big><big>)</big><a class="headerlink" href="#object.__getinitargs__" title="Permalink to this definition">¶</a></dt>
<dd>When a pickled class instance is unpickled, its <a title="object.__init__" class="reference external" href="../reference/datamodel.html#object.__init__"><tt class="xref docutils literal"><span class="pre">__init__()</span></tt></a> method is
normally <em>not</em> invoked.  If it is desirable that the <a title="object.__init__" class="reference external" href="../reference/datamodel.html#object.__init__"><tt class="xref docutils literal"><span class="pre">__init__()</span></tt></a> method
be called on unpickling, an old-style class can define a method
<a title="object.__getinitargs__" class="reference internal" href="#object.__getinitargs__"><tt class="xref docutils literal"><span class="pre">__getinitargs__()</span></tt></a>, which should return a <em>tuple</em> containing the
arguments to be passed to the class constructor (<a title="object.__init__" class="reference external" href="../reference/datamodel.html#object.__init__"><tt class="xref docutils literal"><span class="pre">__init__()</span></tt></a> for
example).  The <a title="object.__getinitargs__" class="reference internal" href="#object.__getinitargs__"><tt class="xref docutils literal"><span class="pre">__getinitargs__()</span></tt></a> method is called at pickle time; the
tuple it returns is incorporated in the pickle for the instance.</dd></dl>

<dl class="method">
<dt id="object.__getnewargs__">
<tt class="descclassname">object.</tt><tt class="descname">__getnewargs__</tt><big>(</big><big>)</big><a class="headerlink" href="#object.__getnewargs__" title="Permalink to this definition">¶</a></dt>
<dd><p>New-style types can provide a <a title="object.__getnewargs__" class="reference internal" href="#object.__getnewargs__"><tt class="xref docutils literal"><span class="pre">__getnewargs__()</span></tt></a> 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 <a title="object.__new__" class="reference external" href="../reference/datamodel.html#object.__new__"><tt class="xref docutils literal"><span class="pre">__new__()</span></tt></a> method for the type
(as it is for tuples and strings).  Instances of a <a class="reference external" href="../glossary.html#term-new-style-class"><em class="xref">new-style class</em></a>
<tt class="docutils literal"><span class="pre">C</span></tt> are created using</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">obj</span> <span class="o">=</span> <span class="n">C</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">C</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>
</pre></div>
</div>
<p>where <em>args</em> is the result of calling <a title="object.__getnewargs__" class="reference internal" href="#object.__getnewargs__"><tt class="xref docutils literal"><span class="pre">__getnewargs__()</span></tt></a> on the original
object; if there is no <a title="object.__getnewargs__" class="reference internal" href="#object.__getnewargs__"><tt class="xref docutils literal"><span class="pre">__getnewargs__()</span></tt></a>, an empty tuple is assumed.</p>
</dd></dl>

<dl class="method">
<dt id="object.__getstate__">
<tt class="descclassname">object.</tt><tt class="descname">__getstate__</tt><big>(</big><big>)</big><a class="headerlink" href="#object.__getstate__" title="Permalink to this definition">¶</a></dt>
<dd>Classes can further influence how their instances are pickled; if the class
defines the method <a title="object.__getstate__" class="reference internal" href="#object.__getstate__"><tt class="xref docutils literal"><span class="pre">__getstate__()</span></tt></a>, it is called and the return state is
pickled as the contents for the instance, instead of the contents of the
instance&#8217;s dictionary.  If there is no <a title="object.__getstate__" class="reference internal" href="#object.__getstate__"><tt class="xref docutils literal"><span class="pre">__getstate__()</span></tt></a> method, the
instance&#8217;s <a title="object.__dict__" class="reference external" href="stdtypes.html#object.__dict__"><tt class="xref docutils literal"><span class="pre">__dict__</span></tt></a> is pickled.</dd></dl>

<dl class="method">
<dt id="object.__setstate__">
<tt class="descclassname">object.</tt><tt class="descname">__setstate__</tt><big>(</big><big>)</big><a class="headerlink" href="#object.__setstate__" title="Permalink to this definition">¶</a></dt>
<dd><p>Upon unpickling, if the class also defines the method <a title="object.__setstate__" class="reference internal" href="#object.__setstate__"><tt class="xref docutils literal"><span class="pre">__setstate__()</span></tt></a>,
it is called with the unpickled state. <a class="footnote-reference" href="#id15" id="id5">[5]</a> If there is no
<a title="object.__setstate__" class="reference internal" href="#object.__setstate__"><tt class="xref docutils literal"><span class="pre">__setstate__()</span></tt></a> method, the pickled state must be a dictionary and its
items are assigned to the new instance&#8217;s dictionary.  If a class defines both
<a title="object.__getstate__" class="reference internal" href="#object.__getstate__"><tt class="xref docutils literal"><span class="pre">__getstate__()</span></tt></a> and <a title="object.__setstate__" class="reference internal" href="#object.__setstate__"><tt class="xref docutils literal"><span class="pre">__setstate__()</span></tt></a>, the state object needn&#8217;t be a
dictionary and these methods can do what they want. <a class="footnote-reference" href="#id16" id="id6">[6]</a></p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For <a class="reference external" href="../glossary.html#term-new-style-class"><em class="xref">new-style class</em></a>es, if <a title="object.__getstate__" class="reference internal" href="#object.__getstate__"><tt class="xref docutils literal"><span class="pre">__getstate__()</span></tt></a> returns a false
value, the <a title="object.__setstate__" class="reference internal" href="#object.__setstate__"><tt class="xref docutils literal"><span class="pre">__setstate__()</span></tt></a> method will not be called.</p>
</div>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">At unpickling time, some methods like <a title="object.__getattr__" class="reference external" href="../reference/datamodel.html#object.__getattr__"><tt class="xref docutils literal"><span class="pre">__getattr__()</span></tt></a>,
<a title="object.__getattribute__" class="reference external" href="../reference/datamodel.html#object.__getattribute__"><tt class="xref docutils literal"><span class="pre">__getattribute__()</span></tt></a>, or <a title="object.__setattr__" class="reference external" href="../reference/datamodel.html#object.__setattr__"><tt class="xref docutils literal"><span class="pre">__setattr__()</span></tt></a> may be called upon the
instance.  In case those methods rely on some internal invariant being
true, the type should implement either <a title="object.__getinitargs__" class="reference internal" href="#object.__getinitargs__"><tt class="xref docutils literal"><span class="pre">__getinitargs__()</span></tt></a> or
<a title="object.__getnewargs__" class="reference internal" href="#object.__getnewargs__"><tt class="xref docutils literal"><span class="pre">__getnewargs__()</span></tt></a> to establish such an invariant; otherwise, neither
<a title="object.__new__" class="reference external" href="../reference/datamodel.html#object.__new__"><tt class="xref docutils literal"><span class="pre">__new__()</span></tt></a> nor <a title="object.__init__" class="reference external" href="../reference/datamodel.html#object.__init__"><tt class="xref docutils literal"><span class="pre">__init__()</span></tt></a> will be called.</p>
</div>
</div>
<div class="section" id="pickling-and-unpickling-extension-types">
<h3>12.1.5.2. Pickling and unpickling extension types<a class="headerlink" href="#pickling-and-unpickling-extension-types" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="object.__reduce__">
<tt class="descclassname">object.</tt><tt class="descname">__reduce__</tt><big>(</big><big>)</big><a class="headerlink" href="#object.__reduce__" title="Permalink to this definition">¶</a></dt>
<dd><p>When the <tt class="xref docutils literal"><span class="pre">Pickler</span></tt> encounters an object of a type it knows nothing
about &#8212; such as an extension type &#8212; it looks in two places for a hint of
how to pickle it.  One alternative is for the object to implement a
<a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a> method.  If provided, at pickling time <a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a>
will be called with no arguments, and it must return either a string or a
tuple.</p>
<p>If a string is returned, it names a global variable whose contents are
pickled as normal.  The string returned by <a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a> should be the
object&#8217;s local name relative to its module; the pickle module searches the
module namespace to determine the object&#8217;s module.</p>
<p>When a tuple is returned, it must be between two and five elements long.
Optional elements can either be omitted, or <tt class="xref docutils literal"><span class="pre">None</span></tt> can be provided as their
value.  The contents of this tuple are pickled as normal and used to
reconstruct the object at unpickling time.  The semantics of each element
are:</p>
<ul>
<li><p class="first">A callable object that will be called to create the initial version of the
object.  The next element of the tuple will provide arguments for this
callable, and later elements provide additional state information that will
subsequently be used to fully reconstruct the pickled data.</p>
<p>In the unpickling environment this object must be either a class, a
callable registered as a &#8220;safe constructor&#8221; (see below), or it must have an
attribute <tt class="xref docutils literal"><span class="pre">__safe_for_unpickling__</span></tt> with a true value. Otherwise, an
<tt class="xref docutils literal"><span class="pre">UnpicklingError</span></tt> will be raised in the unpickling environment.  Note
that as usual, the callable itself is pickled by name.</p>
</li>
<li><p class="first">A tuple of arguments for the callable object.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.5: </span>Formerly, this argument could also be <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
</li>
<li><p class="first">Optionally, the object&#8217;s state, which will be passed to the object&#8217;s
<a title="object.__setstate__" class="reference internal" href="#object.__setstate__"><tt class="xref docutils literal"><span class="pre">__setstate__()</span></tt></a> method as described in section <a class="reference internal" href="#pickle-inst"><em>Pickling and unpickling normal class instances</em></a>.  If
the object has no <a title="object.__setstate__" class="reference internal" href="#object.__setstate__"><tt class="xref docutils literal"><span class="pre">__setstate__()</span></tt></a> method, then, as above, the value
must be a dictionary and it will be added to the object&#8217;s <a title="object.__dict__" class="reference external" href="stdtypes.html#object.__dict__"><tt class="xref docutils literal"><span class="pre">__dict__</span></tt></a>.</p>
</li>
<li><p class="first">Optionally, an iterator (and not a sequence) yielding successive list
items.  These list items will be pickled, and appended to the object using
either <tt class="docutils literal"><span class="pre">obj.append(item)</span></tt> or <tt class="docutils literal"><span class="pre">obj.extend(list_of_items)</span></tt>.  This is
primarily used for list subclasses, but may be used by other classes as
long as they have <tt class="xref docutils literal"><span class="pre">append()</span></tt> and <tt class="xref docutils literal"><span class="pre">extend()</span></tt> methods with the
appropriate signature.  (Whether <tt class="xref docutils literal"><span class="pre">append()</span></tt> or <tt class="xref docutils literal"><span class="pre">extend()</span></tt> is used
depends on which pickle protocol version is used as well as the number of
items to append, so both must be supported.)</p>
</li>
<li><p class="first">Optionally, an iterator (not a sequence) yielding successive dictionary
items, which should be tuples of the form <tt class="docutils literal"><span class="pre">(key,</span> <span class="pre">value)</span></tt>.  These items
will be pickled and stored to the object using <tt class="docutils literal"><span class="pre">obj[key]</span> <span class="pre">=</span> <span class="pre">value</span></tt>. This
is primarily used for dictionary subclasses, but may be used by other
classes as long as they implement <a title="object.__setitem__" class="reference external" href="../reference/datamodel.html#object.__setitem__"><tt class="xref docutils literal"><span class="pre">__setitem__()</span></tt></a>.</p>
</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="object.__reduce_ex__">
<tt class="descclassname">object.</tt><tt class="descname">__reduce_ex__</tt><big>(</big><em>protocol</em><big>)</big><a class="headerlink" href="#object.__reduce_ex__" title="Permalink to this definition">¶</a></dt>
<dd><p>It is sometimes useful to know the protocol version when implementing
<a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a>.  This can be done by implementing a method named
<a title="object.__reduce_ex__" class="reference internal" href="#object.__reduce_ex__"><tt class="xref docutils literal"><span class="pre">__reduce_ex__()</span></tt></a> instead of <a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a>. <a title="object.__reduce_ex__" class="reference internal" href="#object.__reduce_ex__"><tt class="xref docutils literal"><span class="pre">__reduce_ex__()</span></tt></a>,
when it exists, is called in preference over <a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a> (you may
still provide <a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a> for backwards compatibility).  The
<a title="object.__reduce_ex__" class="reference internal" href="#object.__reduce_ex__"><tt class="xref docutils literal"><span class="pre">__reduce_ex__()</span></tt></a> method will be called with a single integer argument,
the protocol version.</p>
<p>The <a title="object" class="reference external" href="functions.html#object"><tt class="xref docutils literal"><span class="pre">object</span></tt></a> class implements both <a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a> and
<a title="object.__reduce_ex__" class="reference internal" href="#object.__reduce_ex__"><tt class="xref docutils literal"><span class="pre">__reduce_ex__()</span></tt></a>; however, if a subclass overrides <a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a>
but not <a title="object.__reduce_ex__" class="reference internal" href="#object.__reduce_ex__"><tt class="xref docutils literal"><span class="pre">__reduce_ex__()</span></tt></a>, the <a title="object.__reduce_ex__" class="reference internal" href="#object.__reduce_ex__"><tt class="xref docutils literal"><span class="pre">__reduce_ex__()</span></tt></a> implementation
detects this and calls <a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a>.</p>
</dd></dl>

<p>An alternative to implementing a <a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a> method on the object to be
pickled, is to register the callable with the <a title="Register pickle support functions." class="reference external" href="copy_reg.html#module-copy_reg"><tt class="xref docutils literal"><span class="pre">copy_reg</span></tt></a> module.  This
module provides a way for programs to register &#8220;reduction functions&#8221; and
constructors for user-defined types.   Reduction functions have the same
semantics and interface as the <a title="object.__reduce__" class="reference internal" href="#object.__reduce__"><tt class="xref docutils literal"><span class="pre">__reduce__()</span></tt></a> method described above, except
that they are called with a single argument, the object to be pickled.</p>
<p>The registered constructor is deemed a &#8220;safe constructor&#8221; for purposes of
unpickling as described above.</p>
</div>
<div class="section" id="pickling-and-unpickling-external-objects">
<h3>12.1.5.3. Pickling and unpickling external objects<a class="headerlink" href="#pickling-and-unpickling-external-objects" title="Permalink to this headline">¶</a></h3>
<p id="index-494">For the benefit of object persistence, the <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module supports the
notion of a reference to an object outside the pickled data stream.  Such
objects are referenced by a &#8220;persistent id&#8221;, which is just an arbitrary string
of printable ASCII characters. The resolution of such names is not defined by
the <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module; it will delegate this resolution to user defined
functions on the pickler and unpickler. <a class="footnote-reference" href="#id17" id="id7">[7]</a></p>
<p>To define external persistent id resolution, you need to set the
<tt class="xref docutils literal"><span class="pre">persistent_id</span></tt> attribute of the pickler object and the
<tt class="xref docutils literal"><span class="pre">persistent_load</span></tt> attribute of the unpickler object.</p>
<p>To pickle objects that have an external persistent id, the pickler must have a
custom <tt class="xref docutils literal"><span class="pre">persistent_id()</span></tt> method that takes an object as an argument and
returns either <tt class="xref docutils literal"><span class="pre">None</span></tt> or the persistent id for that object.  When <tt class="xref docutils literal"><span class="pre">None</span></tt> is
returned, the pickler simply pickles the object as normal.  When a persistent id
string is returned, the pickler will pickle that string, along with a marker so
that the unpickler will recognize the string as a persistent id.</p>
<p>To unpickle external objects, the unpickler must have a custom
<tt class="xref docutils literal"><span class="pre">persistent_load()</span></tt> function that takes a persistent id string and returns
the referenced object.</p>
<p>Here&#8217;s a silly example that <em>might</em> shed more light:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pickle</span>
<span class="kn">from</span> <span class="nn">cStringIO</span> <span class="kn">import</span> <span class="n">StringIO</span>

<span class="n">src</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">Pickler</span><span class="p">(</span><span class="n">src</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">persistent_id</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s">&#39;x&#39;</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;the value </span><span class="si">%d</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">obj</span><span class="o">.</span><span class="n">x</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">None</span>

<span class="n">p</span><span class="o">.</span><span class="n">persistent_id</span> <span class="o">=</span> <span class="n">persistent_id</span>

<span class="k">class</span> <span class="nc">Integer</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span>
    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;My name is integer </span><span class="si">%d</span><span class="s">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span>

<span class="n">i</span> <span class="o">=</span> <span class="n">Integer</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="k">print</span> <span class="n">i</span>
<span class="n">p</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>

<span class="n">datastream</span> <span class="o">=</span> <span class="n">src</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
<span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">datastream</span><span class="p">)</span>
<span class="n">dst</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="n">datastream</span><span class="p">)</span>

<span class="n">up</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">Unpickler</span><span class="p">(</span><span class="n">dst</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">FancyInteger</span><span class="p">(</span><span class="n">Integer</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;I am the integer </span><span class="si">%d</span><span class="s">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span>

<span class="k">def</span> <span class="nf">persistent_load</span><span class="p">(</span><span class="n">persid</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">persid</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;the value &#39;</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">persid</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">2</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">FancyInteger</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">pickle</span><span class="o">.</span><span class="n">UnpicklingError</span><span class="p">,</span> <span class="s">&#39;Invalid persistent id&#39;</span>

<span class="n">up</span><span class="o">.</span><span class="n">persistent_load</span> <span class="o">=</span> <span class="n">persistent_load</span>

<span class="n">j</span> <span class="o">=</span> <span class="n">up</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>
<span class="k">print</span> <span class="n">j</span>
</pre></div>
</div>
<p>In the <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> module, the unpickler&#8217;s <tt class="xref docutils literal"><span class="pre">persistent_load</span></tt> attribute
can also be set to a Python list, in which case, when the unpickler reaches a
persistent id, the persistent id string will simply be appended to this list.
This functionality exists so that a pickle data stream can be &#8220;sniffed&#8221; for
object references without actually instantiating all the objects in a pickle.
<a class="footnote-reference" href="#id18" id="id8">[8]</a>  Setting <tt class="xref docutils literal"><span class="pre">persistent_load</span></tt> to a list is usually used in conjunction
with the <tt class="xref docutils literal"><span class="pre">noload()</span></tt> method on the Unpickler.</p>
</div>
</div>
<div class="section" id="subclassing-unpicklers">
<span id="pickle-sub"></span><h2>12.1.6. Subclassing Unpicklers<a class="headerlink" href="#subclassing-unpicklers" title="Permalink to this headline">¶</a></h2>
<p id="index-495">By default, unpickling will import any class that it finds in the pickle data.
You can control exactly what gets unpickled and what gets called by customizing
your unpickler.  Unfortunately, exactly how you do this is different depending
on whether you&#8217;re using <tt class="xref docutils literal"><span class="pre">pickle</span></tt> or <tt class="xref docutils literal"><span class="pre">cPickle</span></tt>. <a class="footnote-reference" href="#id19" id="id9">[9]</a></p>
<p>In the <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module, you need to derive a subclass from
<tt class="xref docutils literal"><span class="pre">Unpickler</span></tt>, overriding the <tt class="xref docutils literal"><span class="pre">load_global()</span></tt> method.
<tt class="xref docutils literal"><span class="pre">load_global()</span></tt> should read two lines from the pickle data stream where the
first line will the name of the module containing the class and the second line
will be the name of the instance&#8217;s class.  It then looks up the class, possibly
importing the module and digging out the attribute, then it appends what it
finds to the unpickler&#8217;s stack.  Later on, this class will be assigned to the
<tt class="xref docutils literal"><span class="pre">__class__</span></tt> attribute of an empty class, as a way of magically creating an
instance without calling its class&#8217;s <a title="object.__init__" class="reference external" href="../reference/datamodel.html#object.__init__"><tt class="xref docutils literal"><span class="pre">__init__()</span></tt></a>. Your job (should you
choose to accept it), would be to have <tt class="xref docutils literal"><span class="pre">load_global()</span></tt> push onto the
unpickler&#8217;s stack, a known safe version of any class you deem safe to unpickle.
It is up to you to produce such a class.  Or you could raise an error if you
want to disallow all unpickling of instances.  If this sounds like a hack,
you&#8217;re right.  Refer to the source code to make this work.</p>
<p>Things are a little cleaner with <tt class="xref docutils literal"><span class="pre">cPickle</span></tt>, but not by much. To control
what gets unpickled, you can set the unpickler&#8217;s <tt class="xref docutils literal"><span class="pre">find_global</span></tt> attribute
to a function or <tt class="xref docutils literal"><span class="pre">None</span></tt>.  If it is <tt class="xref docutils literal"><span class="pre">None</span></tt> then any attempts to unpickle
instances will raise an <tt class="xref docutils literal"><span class="pre">UnpicklingError</span></tt>.  If it is a function, then it
should accept a module name and a class name, and return the corresponding class
object.  It is responsible for looking up the class and performing any necessary
imports, and it may raise an error to prevent instances of the class from being
unpickled.</p>
<p>The moral of the story is that you should be really careful about the source of
the strings your application unpickles.</p>
</div>
<div class="section" id="example">
<span id="pickle-example"></span><h2>12.1.7. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<p>For the simplest code, use the <tt class="xref docutils literal"><span class="pre">dump()</span></tt> and <tt class="xref docutils literal"><span class="pre">load()</span></tt> functions.  Note
that a self-referencing list is pickled and restored correctly.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pickle</span>

<span class="n">data1</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;a&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="o">+</span><span class="mi">6</span><span class="n">j</span><span class="p">],</span>
         <span class="s">&#39;b&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;string&#39;</span><span class="p">,</span> <span class="s">u&#39;Unicode string&#39;</span><span class="p">),</span>
         <span class="s">&#39;c&#39;</span><span class="p">:</span> <span class="bp">None</span><span class="p">}</span>

<span class="n">selfref_list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">selfref_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">selfref_list</span><span class="p">)</span>

<span class="n">output</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;data.pkl&#39;</span><span class="p">,</span> <span class="s">&#39;wb&#39;</span><span class="p">)</span>

<span class="c"># Pickle dictionary using protocol 0.</span>
<span class="n">pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">data1</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span>

<span class="c"># Pickle the list using the highest protocol available.</span>
<span class="n">pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">selfref_list</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>

<span class="n">output</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>The following example reads the resulting pickled data.  When reading a
pickle-containing file, you should open the file in binary mode because you
can&#8217;t be sure if the ASCII or binary format was used.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pprint</span><span class="o">,</span> <span class="nn">pickle</span>

<span class="n">pkl_file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;data.pkl&#39;</span><span class="p">,</span> <span class="s">&#39;rb&#39;</span><span class="p">)</span>

<span class="n">data1</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">pkl_file</span><span class="p">)</span>
<span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">data1</span><span class="p">)</span>

<span class="n">data2</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">pkl_file</span><span class="p">)</span>
<span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">data2</span><span class="p">)</span>

<span class="n">pkl_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Here&#8217;s a larger example that shows how to modify pickling behavior for a class.
The <tt class="xref docutils literal"><span class="pre">TextReader</span></tt> class opens a text file, and returns the line number and
line contents each time its <tt class="xref docutils literal"><span class="pre">readline()</span></tt> method is called. If a
<tt class="xref docutils literal"><span class="pre">TextReader</span></tt> instance is pickled, all attributes <em>except</em> the file object
member are saved. When the instance is unpickled, the file is reopened, and
reading resumes from the last location. The <a title="object.__setstate__" class="reference internal" href="#object.__setstate__"><tt class="xref docutils literal"><span class="pre">__setstate__()</span></tt></a> and
<a title="object.__getstate__" class="reference internal" href="#object.__getstate__"><tt class="xref docutils literal"><span class="pre">__getstate__()</span></tt></a> methods are used to implement this behavior.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c">#!/usr/bin/python</span>

<span class="k">class</span> <span class="nc">TextReader</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Print and number lines in a text file.&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">file</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">file</span> <span class="o">=</span> <span class="nb">file</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fh</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="nb">file</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lineno</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">readline</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lineno</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lineno</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="n">line</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">None</span>
        <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">):</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lineno</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">odict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> <span class="c"># copy the dict since we change it</span>
        <span class="k">del</span> <span class="n">odict</span><span class="p">[</span><span class="s">&#39;fh&#39;</span><span class="p">]</span>              <span class="c"># remove filehandle entry</span>
        <span class="k">return</span> <span class="n">odict</span>

    <span class="k">def</span> <span class="nf">__setstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
        <span class="n">fh</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="nb">dict</span><span class="p">[</span><span class="s">&#39;file&#39;</span><span class="p">])</span>      <span class="c"># reopen file</span>
        <span class="n">count</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">[</span><span class="s">&#39;lineno&#39;</span><span class="p">]</span>       <span class="c"># read from file...</span>
        <span class="k">while</span> <span class="n">count</span><span class="p">:</span>                 <span class="c"># until line count is restored</span>
            <span class="n">fh</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
            <span class="n">count</span> <span class="o">=</span> <span class="n">count</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="nb">dict</span><span class="p">)</span>   <span class="c"># update attributes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fh</span> <span class="o">=</span> <span class="n">fh</span>                 <span class="c"># save the file object</span>
</pre></div>
</div>
<p>A sample usage might be something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">TextReader</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span> <span class="o">=</span> <span class="n">TextReader</span><span class="o">.</span><span class="n">TextReader</span><span class="p">(</span><span class="s">&quot;TextReader.py&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="go">&#39;1: #!/usr/bin/python&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="go">&#39;2: &#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="go">&#39;3: class TextReader:&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pickle</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;save.p&#39;</span><span class="p">,</span> <span class="s">&#39;wb&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>If you want to see that <tt class="xref docutils literal"><span class="pre">pickle</span></tt> works across Python processes, start
another Python session, before continuing.  What follows can happen from either
the same process or a new process.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pickle</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reader</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s">&#39;save.p&#39;</span><span class="p">,</span> <span class="s">&#39;rb&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reader</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="go">&#39;4:     &quot;&quot;&quot;Print and number lines in a text file.&quot;&quot;&quot;&#39;</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a title="Register pickle support functions." class="reference external" href="copy_reg.html#module-copy_reg"><tt class="xref docutils literal"><span class="pre">copy_reg</span></tt></a></dt>
<dd>Pickle interface constructor registration for extension types.</dd>
<dt>Module <a title="Python object persistence." class="reference external" href="shelve.html#module-shelve"><tt class="xref docutils literal"><span class="pre">shelve</span></tt></a></dt>
<dd>Indexed databases of objects; uses <tt class="xref docutils literal"><span class="pre">pickle</span></tt>.</dd>
<dt>Module <a title="Shallow and deep copy operations." class="reference external" href="copy.html#module-copy"><tt class="xref docutils literal"><span class="pre">copy</span></tt></a></dt>
<dd>Shallow and deep object copying.</dd>
<dt>Module <a title="Convert Python objects to streams of bytes and back (with different constraints)." class="reference external" href="marshal.html#module-marshal"><tt class="xref docutils literal"><span class="pre">marshal</span></tt></a></dt>
<dd>High-performance serialization of built-in types.</dd>
</dl>
</div>
</div>
</div>
<div class="section" id="module-cPickle">
<h1>12.2. <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> &#8212; A faster <tt class="xref docutils literal"><span class="pre">pickle</span></tt><a class="headerlink" href="#module-cPickle" title="Permalink to this headline">¶</a></h1>
<p id="index-496">The <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> module supports serialization and de-serialization of Python
objects, providing an interface and functionality nearly identical to the
<tt class="xref docutils literal"><span class="pre">pickle</span></tt> module.  There are several differences, the most important being
performance and subclassability.</p>
<p>First, <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> can be up to 1000 times faster than <tt class="xref docutils literal"><span class="pre">pickle</span></tt> because
the former is implemented in C.  Second, in the <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> module the
callables <tt class="xref docutils literal"><span class="pre">Pickler()</span></tt> and <tt class="xref docutils literal"><span class="pre">Unpickler()</span></tt> are functions, not classes.
This means that you cannot use them to derive custom pickling and unpickling
subclasses.  Most applications have no need for this functionality and should
benefit from the greatly improved performance of the <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> module.</p>
<p>The pickle data stream produced by <tt class="xref docutils literal"><span class="pre">pickle</span></tt> and <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> are
identical, so it is possible to use <tt class="xref docutils literal"><span class="pre">pickle</span></tt> and <tt class="xref docutils literal"><span class="pre">cPickle</span></tt>
interchangeably with existing pickles. <a class="footnote-reference" href="#id20" id="id10">[10]</a></p>
<p>There are additional minor differences in API between <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> and
<tt class="xref docutils literal"><span class="pre">pickle</span></tt>, however for most applications, they are interchangeable.  More
documentation is provided in the <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module documentation, which
includes a list of the documented differences.</p>
<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="id11" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Don&#8217;t confuse this with the <a title="Convert Python objects to streams of bytes and back (with different constraints)." class="reference external" href="marshal.html#module-marshal"><tt class="xref docutils literal"><span class="pre">marshal</span></tt></a> module</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id12" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[2]</a></td><td>In the <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module these callables are classes, which you could
subclass to customize the behavior.  However, in the <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> module these
callables are factory functions and so cannot be subclassed.  One common reason
to subclass is to control what objects can actually be unpickled.  See section
<a class="reference internal" href="#pickle-sub"><em>Subclassing Unpicklers</em></a> for more details.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id13" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id3">[3]</a></td><td><em>Warning</em>: this is intended for pickling multiple objects without intervening
modifications to the objects or their parts.  If you modify an object and then
pickle it again using the same <tt class="xref docutils literal"><span class="pre">Pickler</span></tt> instance, the object is not
pickled again &#8212; a reference to it is pickled and the <tt class="xref docutils literal"><span class="pre">Unpickler</span></tt> will
return the old value, not the modified one. There are two problems here: (1)
detecting changes, and (2) marshalling a minimal set of changes.  Garbage
Collection may also become a problem here.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id14" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id4">[4]</a></td><td>The exception raised will likely be an <a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a> or an
<a title="exceptions.AttributeError" class="reference external" href="exceptions.html#exceptions.AttributeError"><tt class="xref docutils literal"><span class="pre">AttributeError</span></tt></a> but it could be something else.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id15" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id5">[5]</a></td><td>These methods can also be used to implement copying class instances.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id16" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id6">[6]</a></td><td>This protocol is also used by the shallow and deep copying operations defined in
the <a title="Shallow and deep copy operations." class="reference external" href="copy.html#module-copy"><tt class="xref docutils literal"><span class="pre">copy</span></tt></a> module.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id17" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id7">[7]</a></td><td>The actual mechanism for associating these user defined functions is slightly
different for <tt class="xref docutils literal"><span class="pre">pickle</span></tt> and <tt class="xref docutils literal"><span class="pre">cPickle</span></tt>.  The description given here
works the same for both implementations.  Users of the <tt class="xref docutils literal"><span class="pre">pickle</span></tt> module
could also use subclassing to effect the same results, overriding the
<tt class="xref docutils literal"><span class="pre">persistent_id()</span></tt> and <tt class="xref docutils literal"><span class="pre">persistent_load()</span></tt> methods in the derived
classes.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id18" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id8">[8]</a></td><td>We&#8217;ll leave you with the image of Guido and Jim sitting around sniffing pickles
in their living rooms.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id19" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id9">[9]</a></td><td>A word of caution: the mechanisms described here use internal attributes and
methods, which are subject to change in future versions of Python.  We intend to
someday provide a common interface for controlling this behavior, which will
work in either <tt class="xref docutils literal"><span class="pre">pickle</span></tt> or <tt class="xref docutils literal"><span class="pre">cPickle</span></tt>.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id20" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id10">[10]</a></td><td>Since the pickle data format is actually a tiny stack-oriented programming
language, and some freedom is taken in the encodings of certain objects, it is
possible that the two modules produce different data streams for the same input
objects.  However it is guaranteed that they will always be able to read each
other&#8217;s data streams.</td></tr>
</tbody>
</table>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="#">12.1. <tt class="docutils literal"><span class="pre">pickle</span></tt> &#8212; Python object serialization</a><ul>
<li><a class="reference external" href="#relationship-to-other-python-modules">12.1.1. Relationship to other Python modules</a></li>
<li><a class="reference external" href="#data-stream-format">12.1.2. Data stream format</a></li>
<li><a class="reference external" href="#usage">12.1.3. Usage</a></li>
<li><a class="reference external" href="#what-can-be-pickled-and-unpickled">12.1.4. What can be pickled and unpickled?</a></li>
<li><a class="reference external" href="#the-pickle-protocol">12.1.5. The pickle protocol</a><ul>
<li><a class="reference external" href="#pickling-and-unpickling-normal-class-instances">12.1.5.1. Pickling and unpickling normal class instances</a></li>
<li><a class="reference external" href="#pickling-and-unpickling-extension-types">12.1.5.2. Pickling and unpickling extension types</a></li>
<li><a class="reference external" href="#pickling-and-unpickling-external-objects">12.1.5.3. Pickling and unpickling external objects</a></li>
</ul>
</li>
<li><a class="reference external" href="#subclassing-unpicklers">12.1.6. Subclassing Unpicklers</a></li>
<li><a class="reference external" href="#example">12.1.7. Example</a></li>
</ul>
</li>
<li><a class="reference external" href="#module-cPickle">12.2. <tt class="docutils literal"><span class="pre">cPickle</span></tt> &#8212; A faster <tt class="docutils literal"><span class="pre">pickle</span></tt></a></li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="persistence.html"
                                  title="previous chapter">12. Data Persistence</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="copy_reg.html"
                                  title="next chapter">12.3. <tt class="docutils literal docutils literal"><span class="pre">copy_reg</span></tt> &#8212; Register <tt class="docutils literal docutils literal"><span class="pre">pickle</span></tt> support functions</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/pickle.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="copy_reg.html" title="12.3. copy_reg — Register pickle support functions"
             >next</a> |</li>
        <li class="right" >
          <a href="persistence.html" title="12. Data Persistence"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="persistence.html" >12. Data Persistence</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Mar 19, 2010.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5.
    </div>

  </body>
</html>