<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"prev" href=
"node69.html" />
<link rel=
"parent" href=
"pickle-protocol.html" />
<link rel=
"next" href=
"pickle-sub.html" />
<meta name='aesop' content='information'
/>
<title>3.14.5.3 Pickling and unpickling external objects
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"3.14.5.2 Pickling and unpickling"
href=
"node69.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"3.14.5 The pickle protocol"
href=
"pickle-protocol.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"3.14.6 Subclassing Unpicklers"
href=
"pickle-sub.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node69.html">3.14.5.2 Pickling and unpickling
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"pickle-protocol.html">3.14.5 The pickle protocol
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"pickle-sub.html">3.14.6 Subclassing Unpicklers
</A>
<!--End of Navigation Panel-->
<H3><A NAME=
"SECTION0051453000000000000000">
3.14.5.3 Pickling and unpickling external objects
</A>
For the benefit of object persistence, the
<tt class=
"module">pickle
</tt> module
supports the notion of a reference to an object outside the pickled
data stream. Such objects are referenced by a ``persistent id'',
which is just an arbitrary string of printable ASCII characters.
The resolution of such names is not defined by the
<tt class=
"module">pickle
</tt>
module; it will delegate this resolution to user defined functions on
the pickler and unpickler.
<A NAME=
"tex2html20"
HREF=
"#foot8985"><SUP>3.8</SUP></A>
To define external persistent id resolution, you need to set the
<tt class=
"member">persistent_id
</tt> attribute of the pickler object and the
<tt class=
"member">persistent_load
</tt> attribute of the unpickler object.
To pickle objects that have an external persistent id, the pickler
must have a custom
<tt class=
"function">persistent_id()
</tt> method that takes an
object as an argument and returns either
<code>None
</code> or the persistent
id for that object. When
<code>None
</code> 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.
To unpickle external objects, the unpickler must have a custom
<tt class=
"function">persistent_load()
</tt> function that takes a persistent id
string and returns the referenced object.
Here's a silly example that
<em>might
</em> shed more light:
<div class=
"verbatim"><pre>
from cStringIO import StringIO
return 'the value %d' % obj.x
p.persistent_id = persistent_id
return 'My name is integer %d' % self.x
datastream = src.getvalue()
dst = StringIO(datastream)
up = pickle.Unpickler(dst)
class FancyInteger(Integer):
return 'I am the integer %d' % self.x
def persistent_load(persid):
if persid.startswith('the value '):
value = int(persid.split()[
2])
return FancyInteger(value)
raise pickle.UnpicklingError, 'Invalid persistent id'
up.persistent_load = persistent_load
In the
<tt class=
"module">cPickle
</tt> module, the unpickler's
<tt class=
"member">persistent_load
</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 ``sniffed''
for object references without actually instantiating all the objects
in a pickle.
<A NAME=
"tex2html21"
HREF=
"#foot8892"><SUP>3.9</SUP></A> Setting
<tt class=
"member">persistent_load
</tt> to a list is usually used in conjunction with
the
<tt class=
"method">noload()
</tt> method on the Unpickler.
<BR><HR><H4>Footnotes
</H4>
<DT><A NAME=
"foot8985">... unpickler.
</A><A
HREF=
"node70.html#tex2html20"><SUP>3.8</SUP></A></DT>
<DD>The actual mechanism for
associating these user defined functions is slightly different for
<tt class=
"module">pickle
</tt> and
<tt class=
"module">cPickle
</tt>. The description given here
works the same for both implementations. Users of the
<tt class=
"module">pickle
</tt>
module could also use subclassing to effect the same results,
overriding the
<tt class=
"method">persistent_id()
</tt> and
<tt class=
"method">persistent_load()
</tt>
methods in the derived classes.
<DT><A NAME=
"foot8892">... pickle.
</A><A
HREF=
"node70.html#tex2html21"><SUP>3.9</SUP></A></DT>
<DD>We'll leave you with the image of Guido and Jim
sitting around sniffing pickles in their living rooms.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"3.14.5.2 Pickling and unpickling"
href=
"node69.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"3.14.5 The pickle protocol"
href=
"pickle-protocol.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"3.14.6 Subclassing Unpicklers"
href=
"pickle-sub.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node69.html">3.14.5.2 Pickling and unpickling
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"pickle-protocol.html">3.14.5 The pickle protocol
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"pickle-sub.html">3.14.6 Subclassing Unpicklers
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.