3.14.3 Usage

To serialize an object hierarchy, you first create a pickler, then you call the pickler's dump() method. To de-serialize a data stream, you first create an unpickler, then you call the unpickler's load() method. The pickle module provides the following constant:

HIGHEST_PROTOCOL
The highest protocol version available. This value can be passed as a protocol value.

The pickle module provides the following functions to make this process more convenient:

dump( object, file[, protocol[, bin]])
Write a pickled representation of object to the open file object file. This is equivalent to Pickler(file, protocol, bin).dump(object).

If the protocol parameter is ommitted, protocol 0 is used. If protocol is specified as a negative value or HIGHEST_PROTOCOL, the highest protocol version will be used.

Changed in version 2.3: The protocol parameter was added. The bin parameter is deprecated and only provided for backwards compatibility. You should use the protocol parameter instead.

If the optional bin argument is true, the binary pickle format is used; otherwise the (less efficient) text pickle format is used (for backwards compatibility, this is the default).

file must have a write() method that accepts a single string argument. It can thus be a file object opened for writing, a StringIO object, or any other custom object that meets this interface.

load( file)
Read a string from the open file object file and interpret it as a pickle data stream, reconstructing and returning the original object hierarchy. This is equivalent to Unpickler(file).load().

file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return a string. Thus file can be a file object opened for reading, a StringIO object, or any other custom object that meets this interface.

This function automatically determines whether the data stream was written in binary mode or not.

dumps( object[, protocol[, bin]])
Return the pickled representation of the object as a string, instead of writing it to a file.

If the protocol parameter is ommitted, protocol 0 is used. If protocol is specified as a negative value or HIGHEST_PROTOCOL, the highest protocol version will be used.

Changed in version 2.3: The protocol parameter was added. The bin parameter is deprecated and only provided for backwards compatibility. You should use the protocol parameter instead.

If the optional bin argument is true, the binary pickle format is used; otherwise the (less efficient) text pickle format is used (this is the default).

loads( string)
Read a pickled object hierarchy from a string. Characters in the string past the pickled object's representation are ignored.

The pickle module also defines three exceptions:

exception PickleError
A common base class for the other exceptions defined below. This inherits from Exception.

exception PicklingError
This exception is raised when an unpicklable object is passed to the dump() method.

exception UnpicklingError
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) AttributeError, EOFError, ImportError, and IndexError.

The pickle module also exports two callables3.3, Pickler and Unpickler:

class Pickler( file[, protocol[, bin]])
This takes a file-like object to which it will write a pickle data stream.

If the protocol parameter is ommitted, protocol 0 is used. If protocol is specified as a negative value, the highest protocol version will be used.

Changed in version 2.3: The bin parameter is deprecated and only provided for backwards compatibility. You should use the protocol parameter instead.

Optional bin if true, tells the pickler to use the more efficient binary pickle format, otherwise the ASCII format is used (this is the default).

file must have a write() method that accepts a single string argument. It can thus be an open file object, a StringIO object, or any other custom object that meets this interface.

Pickler objects define one (or two) public methods:

dump( object)
Write a pickled representation of object to the open file object given in the constructor. Either the binary or ASCII format will be used, depending on the value of the bin flag passed to the constructor.

clear_memo( )
Clears the pickler's ``memo''. 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.

Note: Prior to Python 2.3, clear_memo() was only available on the picklers created by cPickle. In the pickle module, picklers have an instance variable called memo which is a Python dictionary. So to clear the memo for a pickle module pickler, you could do the following:

mypickler.memo.clear()

Code that does not need to support older versions of Python should simply use clear_memo().

It is possible to make multiple calls to the dump() method of the same Pickler instance. These must then be matched to the same number of calls to the load() method of the corresponding Unpickler instance. If the same object is pickled by multiple dump() calls, the load() will all yield references to the same object3.4.

Unpickler objects are defined as:

class Unpickler( file)
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 Pickler factory.

file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return a string. Thus file can be a file object opened for reading, a StringIO object, or any other custom object that meets this interface.

Unpickler objects have one (or two) public methods:

load( )
Read a pickled object representation from the open file object given in the constructor, and return the reconstituted object hierarchy specified therein.

noload( )
This is just like load() except that it doesn't actually create any objects. This is useful primarily for finding what's called ``persistent ids'' that may be referenced in a pickle data stream. See section 3.14.5 below for more details.

Note: the noload() method is currently only available on Unpickler objects created with the cPickle module. pickle module Unpicklers do not have the noload() method.



Footnotes

... callables3.3
In the pickle module these callables are classes, which you could subclass to customize the behavior. However, in the cPickle modules these callables are factory functions and so cannot be subclassed. One of the common reasons to subclass is to control what objects can actually be unpickled. See section 3.14.6 for more details.
... object3.4
Warning: 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 Pickler instance, the object is not pickled again -- a reference to it is pickled and the Unpickler 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.
See About this document... for information on suggesting changes.