@@ -8,34 +8,33 @@ \section{Standard Module \sectcode{pickle}}
88\indexii {flattening}{objects}
99\indexii {pickling}{objects}
1010
11- \setindexsubitem {(in module pickle)}
1211
13- The \code {pickle} module implements a basic but powerful algorithm for
12+ The \module {pickle} module implements a basic but powerful algorithm for
1413`` pickling'' (a.k.a.\ serializing, marshalling or flattening) nearly
1514arbitrary Python objects. This is the act of converting objects to a
1615stream of bytes (and back: `` unpickling'' ).
1716This is a more primitive notion than
18- persistency --- although \code {pickle} reads and writes file objects,
17+ persistency --- although \module {pickle} reads and writes file objects,
1918it does not handle the issue of naming persistent objects, nor the
2019(even more complicated) area of concurrent access to persistent
21- objects. The \code {pickle} module can transform a complex object into
20+ objects. The \module {pickle} module can transform a complex object into
2221a byte stream and it can transform the byte stream into an object with
2322the same internal structure. The most obvious thing to do with these
2423byte streams is to write them onto a file, but it is also conceivable
2524to send them across a network or store them in a database. The module
26- \code {shelve} provides a simple interface to pickle and unpickle
25+ \module {shelve} provides a simple interface to pickle and unpickle
2726objects on `` dbm'' -style database files.
2827\refstmodindex {shelve}
2928
30- \strong {Note:} The \code {pickle} module is rather slow. A
31- reimplementation of the same algorithm in C , which is up to 1000 times
32- faster, is available as the \code {cPickle}\refbimodindex {cPickle}
29+ \strong {Note:} The \module {pickle} module is rather slow. A
30+ reimplementation of the same algorithm in \C {} , which is up to 1000 times
31+ faster, is available as the \module {cPickle}\refbimodindex {cPickle}
3332module. This has the same interface except that \code {Pickler} and
3433\code {Unpickler} are factory functions, not classes (so they cannot be
35- used as a base class for inheritance).
34+ used as base classes for inheritance).
3635
37- Unlike the built-in module \code {marshal}, \code {pickle} handles the
38- following correctly:
36+ Unlike the built-in module \module {marshal}, \module {pickle} handles
37+ the following correctly:
3938\refbimodindex {marshal}
4039
4140\begin {itemize }
@@ -48,7 +47,7 @@ \section{Standard Module \sectcode{pickle}}
4847
4948\end {itemize }
5049
51- The data format used by \code {pickle} is Python-specific. This has
50+ The data format used by \module {pickle} is Python-specific. This has
5251the advantage that there are no restrictions imposed by external
5352standards such as XDR%
5453\index {XDR}
@@ -57,36 +56,36 @@ \section{Standard Module \sectcode{pickle}}
5756it means that non-Python programs may not be able to reconstruct
5857pickled Python objects.
5958
60- By default, the \code {pickle} data format uses a printable \ASCII {}
59+ By default, the \module {pickle} data format uses a printable \ASCII {}
6160representation. This is slightly more voluminous than a binary
6261representation. The big advantage of using printable \ASCII {} (and of
63- some other characteristics of \code {pickle}'s representation) is that
62+ some other characteristics of \module {pickle}'s representation) is that
6463for debugging or recovery purposes it is possible for a human to read
6564the pickled file with a standard text editor.
6665
6766A binary format, which is slightly more efficient, can be chosen by
6867specifying a nonzero (true) value for the \var {bin} argument to the
69- \code {Pickler} constructor or the \code {dump()} and \code {dumps()}
68+ \class {Pickler} constructor or the \function {dump()} and \function {dumps()}
7069functions. The binary format is not the default because of backwards
7170compatibility with the Python 1.4 pickle module. In a future version,
7271the default may change to binary.
7372
74- The \code {pickle} module doesn't handle code objects, which the
75- \code {marshal} module does. I suppose \code {pickle} could, and maybe
73+ The \module {pickle} module doesn't handle code objects, which the
74+ \module {marshal} module does. I suppose \module {pickle} could, and maybe
7675it should, but there's probably no great need for it right now (as
77- long as \code {marshal} continues to be used for reading and writing
76+ long as \module {marshal} continues to be used for reading and writing
7877code objects), and at least this avoids the possibility of smuggling
7978Trojan horses into a program.
8079\refbimodindex {marshal}
8180
82- For the benefit of persistency modules written using \code {pickle}, it
81+ For the benefit of persistency modules written using \module {pickle}, it
8382supports the notion of a reference to an object outside the pickled
8483data stream. Such objects are referenced by a name, which is an
8584arbitrary string of printable \ASCII {} characters. The resolution of
86- such names is not defined by the \code {pickle} module --- the
85+ such names is not defined by the \module {pickle} module --- the
8786persistent object module will have to implement a method
88- \code {persistent_load()}. To write references to persistent objects,
89- the persistent module must define a method \code {persistent_id()} which
87+ \method {persistent_load()}. To write references to persistent objects,
88+ the persistent module must define a method \method {persistent_id()} which
9089returns either \code {None} or the persistent ID of the object.
9190
9291There are some restrictions on the pickling of class instances.
@@ -96,38 +95,38 @@ \section{Standard Module \sectcode{pickle}}
9695
9796\setindexsubitem {(pickle protocol)}
9897
99- When a pickled class instance is unpickled, its \code {__init__()} method
98+ When a pickled class instance is unpickled, its \method {__init__()} method
10099is normally \emph {not } invoked. \strong {Note:} This is a deviation
101100from previous versions of this module; the change was introduced in
102101Python 1.5b2. The reason for the change is that in many cases it is
103102desirable to have a constructor that requires arguments; it is a
104- (minor) nuisance to have to provide a \code {__getinitargs__()} method.
103+ (minor) nuisance to have to provide a \method {__getinitargs__()} method.
105104
106- If it is desirable that the \code {__init__()} method be called on
107- unpickling, a class can define a method \code {__getinitargs__()},
105+ If it is desirable that the \method {__init__()} method be called on
106+ unpickling, a class can define a method \method {__getinitargs__()},
108107which should return a \emph {tuple } containing the arguments to be
109- passed to the class constructor (\code {__init__()}). This method is
108+ passed to the class constructor (\method {__init__()}). This method is
110109called at pickle time; the tuple it returns is incorporated in the
111110pickle for the instance.
112- \ttindex {__getinitargs__}
113- \ttindex {__init__}
111+ \ttindex {__getinitargs__() }
112+ \ttindex {__init__() }
114113
115114Classes can further influence how their instances are pickled --- if the class
116- defines the method \code {__getstate__()}, it is called and the return
115+ defines the method \method {__getstate__()}, it is called and the return
117116state is pickled as the contents for the instance, and if the class
118- defines the method \code {__setstate__()}, it is called with the
117+ defines the method \method {__setstate__()}, it is called with the
119118unpickled state. (Note that these methods can also be used to
120119implement copying class instances.) If there is no
121- \code {__getstate__()} method, the instance's \code {__dict__} is
122- pickled. If there is no \code {__setstate__()} method, the pickled
120+ \method {__getstate__()} method, the instance's \member {__dict__} is
121+ pickled. If there is no \method {__setstate__()} method, the pickled
123122object must be a dictionary and its items are assigned to the new
124- instance's dictionary. (If a class defines both \code {__getstate__()}
125- and \code {__setstate__()}, the state object needn't be a dictionary
123+ instance's dictionary. (If a class defines both \method {__getstate__()}
124+ and \method {__setstate__()}, the state object needn't be a dictionary
126125--- these methods can do what they want.) This protocol is also used
127- by the shallow and deep copying operations defined in the \code {copy}
128- module.
129- \ttindex {__getstate__}
130- \ttindex {__setstate__}
126+ by the shallow and deep copying operations defined in the \module {copy}
127+ module.\refstmodindex {copy}
128+ \ttindex {__getstate__() }
129+ \ttindex {__setstate__() }
131130\ttindex {__dict__}
132131
133132Note that when class instances are pickled, their class's code and
@@ -137,7 +136,7 @@ \section{Standard Module \sectcode{pickle}}
137136version of the class. If you plan to have long-lived objects that
138137will see many versions of a class, it may be worthwhile to put a version
139138number in the objects so that suitable conversions can be made by the
140- class's \code {__setstate__()} method.
139+ class's \method {__setstate__()} method.
141140
142141When a class itself is pickled, only its name is pickled --- the class
143142definition is not pickled, but re-imported by the unpickling process.
@@ -154,39 +153,39 @@ \section{Standard Module \sectcode{pickle}}
154153p = pickle.Pickler(f)
155154p.dump(x)
156155\end {verbatim }
157- %
156+
158157A shorthand for this is:
159158
160159\begin {verbatim }
161160pickle.dump(x, f)
162161\end {verbatim }
163- %
162+
164163To unpickle an object \code {x} from a file \code {f}, open for reading:
165164
166165\begin {verbatim }
167166u = pickle.Unpickler(f)
168167x = u.load()
169168\end {verbatim }
170- %
169+
171170A shorthand is:
172171
173172\begin {verbatim }
174173x = pickle.load(f)
175174\end {verbatim }
176- %
177- The \code {Pickler} class only calls the method \code {f.write()} with a
178- string argument. The \code {Unpickler} calls the methods \code {f.read()}
175+
176+ The \class {Pickler} class only calls the method \code {f.write()} with a
177+ string argument. The \class {Unpickler} calls the methods \code {f.read()}
179178(with an integer argument) and \code {f.readline()} (without argument),
180179both returning a string. It is explicitly allowed to pass non-file
181180objects here, as long as they have the right methods.
182181\ttindex {Unpickler}
183182\ttindex {Pickler}
184183
185- The constructor for the \code {Pickler} class has an optional second
184+ The constructor for the \class {Pickler} class has an optional second
186185argument, \var {bin}. If this is present and nonzero, the binary
187186pickle format is used; if it is zero or absent, the (less efficient,
188187but backwards compatible) text pickle format is used. The
189- \code {Unpickler} class does not have an argument to distinguish
188+ \class {Unpickler} class does not have an argument to distinguish
190189between binary and text pickle formats; it accepts either format.
191190
192191The following types can be pickled:
@@ -202,45 +201,45 @@ \section{Standard Module \sectcode{pickle}}
202201
203202\item classes that are defined at the top level in a module
204203
205- \item instances of such classes whose \code {__dict__} or
206- \code {__setstate__()} is picklable
204+ \item instances of such classes whose \member {__dict__} or
205+ \method {__setstate__()} is picklable
207206
208207\end {itemize }
209208
210209Attempts to pickle unpicklable objects will raise the
211- \code {PicklingError} exception; when this happens, an unspecified
210+ \exception {PicklingError} exception; when this happens, an unspecified
212211number of bytes may have been written to the file.
213212
214- It is possible to make multiple calls to the \code {dump()} method of
215- the same \code {Pickler} instance. These must then be matched to the
216- same number of calls to the \code {load()} instance of the
217- corresponding \code {Unpickler} instance. If the same object is
218- pickled by multiple \code {dump()} calls, the \code {load()} will all
213+ It is possible to make multiple calls to the \method {dump()} method of
214+ the same \class {Pickler} instance. These must then be matched to the
215+ same number of calls to the \method {load()} method of the
216+ corresponding \class {Unpickler} instance. If the same object is
217+ pickled by multiple \method {dump()} calls, the \method {load()} will all
219218yield references to the same object. \emph {Warning }: this is intended
220219for pickling multiple objects without intervening modifications to the
221220objects or their parts. If you modify an object and then pickle it
222- again using the same \code {Pickler} instance, the object is not
221+ again using the same \class {Pickler} instance, the object is not
223222pickled again --- a reference to it is pickled and the
224- \code {Unpickler} will return the old value, not the modified one.
223+ \class {Unpickler} will return the old value, not the modified one.
225224(There are two problems here: (a) detecting changes, and (b)
226225marshalling a minimal set of changes. I have no answers. Garbage
227226Collection may also become a problem here.)
228227
229- Apart from the \code {Pickler} and \code {Unpickler} classes, the
228+ Apart from the \class {Pickler} and \class {Unpickler} classes, the
230229module defines the following functions, and an exception:
231230
232231\begin {funcdesc }{dump}{object, file\optional {, bin}}
233232Write a pickled representation of \var {obect} to the open file object
234233\var {file}. This is equivalent to
235- \code {Pickler(\var {file}, \var {bin}).dump(\var {object})}.
234+ \samp {Pickler(\var {file}, \var {bin}).dump(\var {object})}.
236235If the optional \var {bin} argument is present and nonzero, the binary
237236pickle format is used; if it is zero or absent, the (less efficient)
238237text pickle format is used.
239238\end {funcdesc }
240239
241240\begin {funcdesc }{load}{file}
242241Read a pickled object from the open file object \var {file}. This is
243- equivalent to \code {Unpickler(\var {file}).load()}.
242+ equivalent to \samp {Unpickler(\var {file}).load()}.
244243\end {funcdesc }
245244
246245\begin {funcdesc }{dumps}{object\optional {, bin}}
@@ -262,6 +261,12 @@ \section{Standard Module \sectcode{pickle}}
262261
263262
264263\begin {seealso }
264+ \seemodule {copy}{shallow and deep object copying}
265+
265266\seemodule [copyreg]{copy_reg}{pickle interface constructor
266267registration}
268+
269+ \seemodule {marshal}{high-performance serialization of built-in types}
270+
271+ \seemodule {shelve}{indexed databases of objects; uses \module {pickle}}
267272\end {seealso }
0 commit comments