📄 module-shelve.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>3.14 shelve -- Python object persistence</title>
<META NAME="description" CONTENT="3.14 shelve -- Python object persistence">
<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="next" href="module-copy.html" tppabs="http://www.python.org/doc/current/lib/module-copy.html">
<LINK REL="previous" href="module-copyreg.html" tppabs="http://www.python.org/doc/current/lib/module-copyreg.html">
<LINK REL="up" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">
<LINK REL="next" href="module-copy.html" tppabs="http://www.python.org/doc/current/lib/module-copy.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-copyreg.html" tppabs="http://www.python.org/doc/current/lib/module-copyreg.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="module-copy.html" tppabs="http://www.python.org/doc/current/lib/module-copy.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-copyreg.html" tppabs="http://www.python.org/doc/current/lib/module-copyreg.html">3.13 copy_reg </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="module-copy.html" tppabs="http://www.python.org/doc/current/lib/module-copy.html">3.15 copy </A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0051400000000000000000">
3.14 <tt class="module">shelve</tt> --
Python object persistence</A>
</H1>
<P>
<P>
A ``shelf'' is a persistent, dictionary-like object. The difference
with ``dbm'' databases is that the values (not the keys!) in a shelf
can be essentially arbitrary Python objects -- anything that the
<tt class='module'><a href="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.html">pickle</a></tt> module can handle. This includes most class
instances, recursive data types, and objects containing lots of shared
sub-objects. The keys are ordinary strings.
<P>
To summarize the interface (<code>key</code> is a string, <code>data</code> is an
arbitrary object):
<P>
<dl><dd><pre class="verbatim">
import shelve
d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
d[key] = data # store data at key (overwrites old data if
# using an existing key)
data = d[key] # retrieve data at key (raise KeyError if no
# such key)
del d[key] # delete data stored at key (raises KeyError
# if no such key)
flag = d.has_key(key) # true if the key exists
list = d.keys() # a list of all existing keys (slow!)
d.close() # close it
</pre></dl>
<P>
Restrictions:
<P>
<UL>
<LI>The choice of which database package will be used
(e.g. <tt class='module'><a href="module-dbm.html" tppabs="http://www.python.org/doc/current/lib/module-dbm.html">dbm</a></tt> or <tt class='module'><a href="module-gdbm.html" tppabs="http://www.python.org/doc/current/lib/module-gdbm.html">gdbm</a></tt>) depends on which interface
is available. Therefore it is not safe to open the database directly
using <tt class='module'><a href="module-dbm.html" tppabs="http://www.python.org/doc/current/lib/module-dbm.html">dbm</a></tt>. The database is also (unfortunately) subject
to the limitations of <tt class='module'><a href="module-dbm.html" tppabs="http://www.python.org/doc/current/lib/module-dbm.html">dbm</a></tt>, if it is used -- this means
that (the pickled representation of) the objects stored in the
database should be fairly small, and in rare cases key collisions may
cause the database to refuse updates.
<P>
</LI>
<LI>Dependent on the implementation, closing a persistent dictionary may
or may not be necessary to flush changes to disk.
<P>
</LI>
<LI>The <tt class="module">shelve</tt> module does not support <i>concurrent</i> read/write
access to shelved objects. (Multiple simultaneous read accesses are
safe.) When a program has a shelf open for writing, no other program
should have it open for reading or writing. Unix file locking can
be used to solve this, but this differs across Unix versions and
requires knowledge about the database implementation used.
<P>
</LI>
</UL>
<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-anydbm.html" tppabs="http://www.python.org/doc/current/lib/module-anydbm.html">anydbm</a></tt>:</b>
<dd>Generic interface to <code>dbm</code>-style databases.
</dl>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-dbhash.html" tppabs="http://www.python.org/doc/current/lib/module-dbhash.html">dbhash</a></tt>:</b>
<dd>BSD <code>db</code> database interface.
</dl>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-dbm.html" tppabs="http://www.python.org/doc/current/lib/module-dbm.html">dbm</a></tt>:</b>
<dd>Standard Unix database interface.
</dl>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-dumbdbm.html" tppabs="http://www.python.org/doc/current/lib/module-dumbdbm.html">dumbdbm</a></tt>:</b>
<dd>Portable implementation of the <code>dbm</code> interface.
</dl>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-gdbm.html" tppabs="http://www.python.org/doc/current/lib/module-gdbm.html">gdbm</a></tt>:</b>
<dd>GNU database interface, based on the <code>dbm</code> interface.
</dl>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.html">pickle</a></tt>:</b>
<dd>Object serialization used by <tt class="module">shelve</tt>.
</dl>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-cPickle.html" tppabs="http://www.python.org/doc/current/lib/module-cPickle.html">cPickle</a></tt>:</b>
<dd>High-performance version of <tt class='module'><a href="module-pickle.html" tppabs="http://www.python.org/doc/current/lib/module-pickle.html">pickle</a></tt>.
</dl>
</div>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-copyreg.html" tppabs="http://www.python.org/doc/current/lib/module-copyreg.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="module-copy.html" tppabs="http://www.python.org/doc/current/lib/module-copy.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-copyreg.html" tppabs="http://www.python.org/doc/current/lib/module-copyreg.html">3.13 copy_reg </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="module-copy.html" tppabs="http://www.python.org/doc/current/lib/module-copy.html">3.15 copy </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 + -