📄 pickle-example.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>3.11.1 Example </title>
<META NAME="description" CONTENT="3.11.1 Example ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="previous" href="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.html">
<LINK REL="up" href="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.html">
<LINK REL="next" href="module-cPickle.html" tppabs="http://www.python.org/doc/current/lib/module-cPickle.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.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="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.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="module-cPickle.html" tppabs="http://www.python.org/doc/current/lib/module-cPickle.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-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.html">3.11 pickle </A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.html">3.11 pickle </A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-cPickle.html" tppabs="http://www.python.org/doc/current/lib/module-cPickle.html">3.12 cPickle </A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H2>
<BR>
3.11.1 Example
</H2>
<P>
Here's a simple example of how to modify pickling behavior for a
class. The <tt class="class">TextReader</tt> class opens a text file, and returns
the line number and line contents each time its <tt class="method">readline()</tt>
method is called. If a <tt class="class">TextReader</tt> instance is pickled, all
attributes <i>except</i> the file object member are saved. When the
instance is unpickled, the file is reopened, and reading resumes from
the last location. The <tt class="method">__setstate__()</tt> and
<tt class="method">__getstate__()</tt> methods are used to implement this behavior.
<P>
<dl><dd><pre class="verbatim">
# illustrate __setstate__ and __getstate__ methods
# used in pickling.
class TextReader:
"Print and number lines in a text file."
def __init__(self,file):
self.file = file
self.fh = open(file,'r')
self.lineno = 0
def readline(self):
self.lineno = self.lineno + 1
line = self.fh.readline()
if not line:
return None
return "%d: %s" % (self.lineno,line[:-1])
# return data representation for pickled object
def __getstate__(self):
odict = self.__dict__ # get attribute dictionary
del odict['fh'] # remove filehandle entry
return odict
# restore object state from data representation generated
# by __getstate__
def __setstate__(self,dict):
fh = open(dict['file']) # reopen file
count = dict['lineno'] # read from file...
while count: # until line count is restored
fh.readline()
count = count - 1
dict['fh'] = fh # create filehandle entry
self.__dict__ = dict # make dict our attribute dictionary
</pre></dl>
<P>
A sample usage might be something like this:
<P>
<dl><dd><pre class="verbatim">
>>> import TextReader
>>> obj = TextReader.TextReader("TextReader.py")
>>> obj.readline()
'1: #!/usr/local/bin/python'
>>> # (more invocations of obj.readline() here)
... obj.readline()
'7: class TextReader:'
>>> import pickle
>>> pickle.dump(obj,open('save.p','w'))
(start another Python session)
>>> import pickle
>>> reader = pickle.load(open('save.p'))
>>> reader.readline()
'8: "Print and number lines in a text file."'
</pre></dl>
<P>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.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="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.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="module-cPickle.html" tppabs="http://www.python.org/doc/current/lib/module-cPickle.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-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.html">3.11 pickle </A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.html">3.11 pickle </A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-cPickle.html" tppabs="http://www.python.org/doc/current/lib/module-cPickle.html">3.12 cPickle </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 + -