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:
- The highest protocol version available. This value can be passed as a protocol value. New in version 2.3.
The pickle module provides the following functions to make this process more convenient:
-
Write a pickled representation of obj to the open file object
file. This is equivalent to
Pickler(file, protocol, bin).dump(obj)
.If the protocol parameter is omitted, 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.
-
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.
-
Return the pickled representation of the object as a string, instead
of writing it to a file.
If the protocol parameter is omitted, 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).
- 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:
- A common base class for the other exceptions defined below. This inherits from Exception.
- This exception is raised when an unpicklable object is passed to the dump() method.
- 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:
-
This takes a file-like object to which it will write a pickle data
stream.
If the protocol parameter is omitted, 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:
- Write a pickled representation of obj 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.
-
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 object.3.4
Unpickler objects are defined as:
-
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:
- Read a pickled object representation from the open file object given in the constructor, and return the reconstituted object hierarchy specified therein.
-
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
- In the pickle module these callables are classes, which you could subclass to customize the behavior. However, in the cPickle 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 3.14.6 for more details.
- 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.