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 pickle module provides the following functions to make this process more convenient:
object, file[, protocol[, bin]]) |
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.
file) |
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.
object[, protocol[, bin]]) |
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).
string) |
The pickle module also defines three exceptions:
The pickle module also exports two callables3.3, Pickler and Unpickler:
file[, protocol[, bin]]) |
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:
object) |
) |
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:
file) |
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:
) |
) |
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.