📄 module-pickle.html
字号:
<dl><dd><pre class="verbatim">
p = pickle.Pickler(f)
p.dump(x)
</pre></dl>
<P>
A shorthand for this is:
<P>
<dl><dd><pre class="verbatim">
pickle.dump(x, f)
</pre></dl>
<P>
To unpickle an object <code>x</code> from a file <code>f</code>, open for reading:
<P>
<dl><dd><pre class="verbatim">
u = pickle.Unpickler(f)
x = u.load()
</pre></dl>
<P>
A shorthand is:
<P>
<dl><dd><pre class="verbatim">
x = pickle.load(f)
</pre></dl>
<P>
The <tt class="class">Pickler</tt> class only calls the method <code>f.write()</code> with a
string argument. The <tt class="class">Unpickler</tt> calls the methods <code>f.read()</code>
(with an integer argument) and <code>f.readline()</code> (without argument),
both returning a string. It is explicitly allowed to pass non-file
objects here, as long as they have the right methods.
<P>
The constructor for the <tt class="class">Pickler</tt> class has an optional second
argument, <var>bin</var>. If this is present and true, the binary
pickle format is used; if it is absent or false, the (less efficient,
but backwards compatible) text pickle format is used. The
<tt class="class">Unpickler</tt> class does not have an argument to distinguish
between binary and text pickle formats; it accepts either format.
<P>
The following types can be pickled:
<P>
<UL>
<LI><code>None</code>
<P>
</LI>
<LI>integers, long integers, floating point numbers
<P>
</LI>
<LI>normal and Unicode strings
<P>
</LI>
<LI>tuples, lists and dictionaries containing only picklable objects
<P>
</LI>
<LI>functions defined at the top level of a module (by name
reference, not storage of the implementation)
<P>
</LI>
<LI>built-in functions
<P>
</LI>
<LI>classes that are defined at the top level in a module
<P>
</LI>
<LI>instances of such classes whose <tt class="member">__dict__</tt> or
<tt class="method">__setstate__()</tt> is picklable
<P>
</LI>
</UL>
<P>
Attempts to pickle unpicklable objects will raise the
<tt class="exception">PicklingError</tt> exception; when this happens, an unspecified
number of bytes may have been written to the file.
<P>
It is possible to make multiple calls to the <tt class="method">dump()</tt> method of
the same <tt class="class">Pickler</tt> instance. These must then be matched to the
same number of calls to the <tt class="method">load()</tt> method of the
corresponding <tt class="class">Unpickler</tt> instance. If the same object is
pickled by multiple <tt class="method">dump()</tt> calls, the <tt class="method">load()</tt> will all
yield references to the same object. <i>Warning</i>: 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 <tt class="class">Pickler</tt> instance, the object is not
pickled again -- a reference to it is pickled and the
<tt class="class">Unpickler</tt> will return the old value, not the modified one.
(There are two problems here: (a) detecting changes, and (b)
marshalling a minimal set of changes. I have no answers. Garbage
Collection may also become a problem here.)
<P>
Apart from the <tt class="class">Pickler</tt> and <tt class="class">Unpickler</tt> classes, the
module defines the following functions, and an exception:
<P>
<dl><dt><b><a name='l2h-415'><tt class='function'>dump</tt></a></b> (<var>object, file</var><big>[</big><var>, bin</var><big>]</big>)
<dd>
Write a pickled representation of <var>object</var> to the open file object
<var>file</var>. This is equivalent to
"<tt class="samp">Pickler(<var>file</var>, <var>bin</var>).dump(<var>object</var>)</tt>".
If the optional <var>bin</var> argument is present and nonzero, the binary
pickle format is used; if it is zero or absent, the (less efficient)
text pickle format is used.
</dl>
<P>
<dl><dt><b><a name='l2h-416'><tt class='function'>load</tt></a></b> (<var>file</var>)
<dd>
Read a pickled object from the open file object <var>file</var>. This is
equivalent to "<tt class="samp">Unpickler(<var>file</var>).load()</tt>".
</dl>
<P>
<dl><dt><b><a name='l2h-417'><tt class='function'>dumps</tt></a></b> (<var>object</var><big>[</big><var>, bin</var><big>]</big>)
<dd>
Return the pickled representation of the object as a string, instead
of writing it to a file. If the optional <var>bin</var> argument is
present and nonzero, the binary pickle format is used; if it is zero
or absent, the (less efficient) text pickle format is used.
</dl>
<P>
<dl><dt><b><a name='l2h-418'><tt class='function'>loads</tt></a></b> (<var>string</var>)
<dd>
Read a pickled object from a string instead of a file. Characters in
the string past the pickled object's representation are ignored.
</dl>
<P>
<dl><dt><b><a name='l2h-419'><tt class='exception'>PicklingError</tt></a></b>
<dd>
This exception is raised when an unpicklable object is passed to
<tt class="method">Pickler.dump()</tt>.
</dl>
<P>
<div class='seealso'>
<p class='heading'><b>See Also:</b></p>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-copyreg.html" tppabs="http://www.python.org/doc/current/lib/module-copyreg.html">copy_reg</a></tt>:</b>
<dd>pickle interface constructor
registration.
</dl>
<P>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-shelve.html" tppabs="http://www.python.org/doc/current/lib/module-shelve.html">shelve</a></tt>:</b>
<dd>indexed databases of objects; uses <tt class="module">pickle</tt>.
</dl>
<P>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-copy.html" tppabs="http://www.python.org/doc/current/lib/module-copy.html">copy</a></tt>:</b>
<dd>shallow and deep object copying.
</dl>
<P>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-marshal.html" tppabs="http://www.python.org/doc/current/lib/module-marshal.html">marshal</a></tt>:</b>
<dd>high-performance serialization of built-in types.
</dl>
</div>
<P>
<p><hr>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html1448"
href="pickle-example.html" tppabs="http://www.python.org/doc/current/lib/pickle-example.html">3.11.1 Example </A>
</UL>
<!--End of Table of Child-Links-->
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-linecache.html" tppabs="http://www.python.org/doc/current/lib/module-linecache.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="pickle-example.html" tppabs="http://www.python.org/doc/current/lib/pickle-example.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-linecache.html" tppabs="http://www.python.org/doc/current/lib/module-linecache.html">3.10 linecache </A>
<b class="navlabel">Up:</b> <a class="sectref" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="pickle-example.html" tppabs="http://www.python.org/doc/current/lib/pickle-example.html">3.11.1 Example</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -