gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

532 lines
40KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="shelve — Python object persistence" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/shelve.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/shelve.py 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 o..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/shelve.py 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 o..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>shelve — Python object persistence &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="marshal — Internal Python object serialization" href="marshal.html" />
  33. <link rel="prev" title="copyreg — Register pickle support functions" href="copyreg.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/shelve.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code> — Python object persistence</a><ul>
  86. <li><a class="reference internal" href="#restrictions">Restrictions</a></li>
  87. <li><a class="reference internal" href="#example">Example</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="copyreg.html"
  95. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copyreg</span></code> — Register <code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> support functions</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="marshal.html"
  100. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code> — Internal Python object serialization</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/library/shelve.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="marshal.html" title="marshal — Internal Python object serialization"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="copyreg.html" title="copyreg — Register pickle support functions"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="persistence.html" accesskey="U">Data Persistence</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code> — Python object persistence</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="module-shelve">
  170. <span id="shelve-python-object-persistence"></span><h1><a class="reference internal" href="#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a> — Python object persistence<a class="headerlink" href="#module-shelve" title="Link to this heading">¶</a></h1>
  171. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/shelve.py">Lib/shelve.py</a></p>
  172. <hr class="docutils" id="index-0" />
  173. <p>A “shelf” is a persistent, dictionary-like object. The difference with “dbm”
  174. databases is that the values (not the keys!) in a shelf can be essentially
  175. arbitrary Python objects — anything that the <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> module can handle.
  176. This includes most class instances, recursive data types, and objects containing
  177. lots of shared sub-objects. The keys are ordinary strings.</p>
  178. <dl class="py function">
  179. <dt class="sig sig-object py" id="shelve.open">
  180. <span class="sig-prename descclassname"><span class="pre">shelve.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'c'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">protocol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">writeback</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shelve.open" title="Link to this definition">¶</a></dt>
  181. <dd><p>Open a persistent dictionary. The filename specified is the base filename for
  182. the underlying database. As a side-effect, an extension may be added to the
  183. filename and more than one file may be created. By default, the underlying
  184. database file is opened for reading and writing. The optional <em>flag</em> parameter
  185. has the same interpretation as the <em>flag</em> parameter of <a class="reference internal" href="dbm.html#dbm.open" title="dbm.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">dbm.open()</span></code></a>.</p>
  186. <p>By default, pickles created with <a class="reference internal" href="pickle.html#pickle.DEFAULT_PROTOCOL" title="pickle.DEFAULT_PROTOCOL"><code class="xref py py-const docutils literal notranslate"><span class="pre">pickle.DEFAULT_PROTOCOL</span></code></a> are used
  187. to serialize values. The version of the pickle protocol can be specified
  188. with the <em>protocol</em> parameter.</p>
  189. <p>Because of Python semantics, a shelf cannot know when a mutable
  190. persistent-dictionary entry is modified. By default modified objects are
  191. written <em>only</em> when assigned to the shelf (see <a class="reference internal" href="#shelve-example"><span class="std std-ref">Example</span></a>). If the
  192. optional <em>writeback</em> parameter is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, all entries accessed are also
  193. cached in memory, and written back on <a class="reference internal" href="#shelve.Shelf.sync" title="shelve.Shelf.sync"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sync()</span></code></a> and
  194. <a class="reference internal" href="#shelve.Shelf.close" title="shelve.Shelf.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a>; this can make it handier to mutate mutable entries in
  195. the persistent dictionary, but, if many entries are accessed, it can consume
  196. vast amounts of memory for the cache, and it can make the close operation
  197. very slow since all accessed entries are written back (there is no way to
  198. determine which accessed entries are mutable, nor which ones were actually
  199. mutated).</p>
  200. <div class="versionchanged">
  201. <p><span class="versionmodified changed">Changed in version 3.10: </span><a class="reference internal" href="pickle.html#pickle.DEFAULT_PROTOCOL" title="pickle.DEFAULT_PROTOCOL"><code class="xref py py-const docutils literal notranslate"><span class="pre">pickle.DEFAULT_PROTOCOL</span></code></a> is now used as the default pickle
  202. protocol.</p>
  203. </div>
  204. <div class="versionchanged">
  205. <p><span class="versionmodified changed">Changed in version 3.11: </span>Accepts <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for filename.</p>
  206. </div>
  207. <div class="admonition note">
  208. <p class="admonition-title">Note</p>
  209. <p>Do not rely on the shelf being closed automatically; always call
  210. <a class="reference internal" href="#shelve.Shelf.close" title="shelve.Shelf.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> explicitly when you don’t need it any more, or
  211. use <a class="reference internal" href="#shelve.open" title="shelve.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">shelve.open()</span></code></a> as a context manager:</p>
  212. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">shelve</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">db</span><span class="p">:</span>
  213. <span class="n">db</span><span class="p">[</span><span class="s1">&#39;eggs&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;eggs&#39;</span>
  214. </pre></div>
  215. </div>
  216. </div>
  217. </dd></dl>
  218. <div class="admonition warning" id="shelve-security">
  219. <p class="admonition-title">Warning</p>
  220. <p>Because the <a class="reference internal" href="#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a> module is backed by <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a>, it is insecure
  221. to load a shelf from an untrusted source. Like with pickle, loading a shelf
  222. can execute arbitrary code.</p>
  223. </div>
  224. <p>Shelf objects support most of methods and operations supported by dictionaries
  225. (except copying, constructors and operators <code class="docutils literal notranslate"><span class="pre">|</span></code> and <code class="docutils literal notranslate"><span class="pre">|=</span></code>). This eases the
  226. transition from dictionary based scripts to those requiring persistent storage.</p>
  227. <p>Two additional methods are supported:</p>
  228. <dl class="py method">
  229. <dt class="sig sig-object py" id="shelve.Shelf.sync">
  230. <span class="sig-prename descclassname"><span class="pre">Shelf.</span></span><span class="sig-name descname"><span class="pre">sync</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shelve.Shelf.sync" title="Link to this definition">¶</a></dt>
  231. <dd><p>Write back all entries in the cache if the shelf was opened with <em>writeback</em>
  232. set to <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>. Also empty the cache and synchronize the persistent
  233. dictionary on disk, if feasible. This is called automatically when the shelf
  234. is closed with <a class="reference internal" href="#shelve.Shelf.close" title="shelve.Shelf.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a>.</p>
  235. </dd></dl>
  236. <dl class="py method">
  237. <dt class="sig sig-object py" id="shelve.Shelf.close">
  238. <span class="sig-prename descclassname"><span class="pre">Shelf.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shelve.Shelf.close" title="Link to this definition">¶</a></dt>
  239. <dd><p>Synchronize and close the persistent <em>dict</em> object. Operations on a closed
  240. shelf will fail with a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  241. </dd></dl>
  242. <div class="admonition seealso">
  243. <p class="admonition-title">See also</p>
  244. <p><a class="reference external" href="https://code.activestate.com/recipes/576642/">Persistent dictionary recipe</a>
  245. with widely supported storage formats and having the speed of native
  246. dictionaries.</p>
  247. </div>
  248. <section id="restrictions">
  249. <h2>Restrictions<a class="headerlink" href="#restrictions" title="Link to this heading">¶</a></h2>
  250. <ul class="simple" id="index-1">
  251. <li><p>The choice of which database package will be used (such as <a class="reference internal" href="dbm.html#module-dbm.ndbm" title="dbm.ndbm: The New Database Manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a> or
  252. <a class="reference internal" href="dbm.html#module-dbm.gnu" title="dbm.gnu: GNU database manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a>) depends on which interface is available. Therefore it is not
  253. safe to open the database directly using <a class="reference internal" href="dbm.html#module-dbm" title="dbm: Interfaces to various Unix &quot;database&quot; formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a>. The database is also
  254. (unfortunately) subject to the limitations of <a class="reference internal" href="dbm.html#module-dbm" title="dbm: Interfaces to various Unix &quot;database&quot; formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a>, if it is used —
  255. this means that (the pickled representation of) the objects stored in the
  256. database should be fairly small, and in rare cases key collisions may cause
  257. the database to refuse updates.</p></li>
  258. <li><p>The <a class="reference internal" href="#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a> module does not support <em>concurrent</em> read/write access to
  259. shelved objects. (Multiple simultaneous read accesses are safe.) When a
  260. program has a shelf open for writing, no other program should have it open for
  261. reading or writing. Unix file locking can be used to solve this, but this
  262. differs across Unix versions and requires knowledge about the database
  263. implementation used.</p></li>
  264. <li><p>On macOS <a class="reference internal" href="dbm.html#module-dbm.ndbm" title="dbm.ndbm: The New Database Manager (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a> can silently corrupt the database file on updates,
  265. which can cause hard crashes when trying to read from the database.</p></li>
  266. </ul>
  267. <dl class="py class">
  268. <dt class="sig sig-object py" id="shelve.Shelf">
  269. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">shelve.</span></span><span class="sig-name descname"><span class="pre">Shelf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dict</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">protocol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">writeback</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">keyencoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'utf-8'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shelve.Shelf" title="Link to this definition">¶</a></dt>
  270. <dd><p>A subclass of <a class="reference internal" href="collections.abc.html#collections.abc.MutableMapping" title="collections.abc.MutableMapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.MutableMapping</span></code></a> which stores pickled
  271. values in the <em>dict</em> object.</p>
  272. <p>By default, pickles created with <a class="reference internal" href="pickle.html#pickle.DEFAULT_PROTOCOL" title="pickle.DEFAULT_PROTOCOL"><code class="xref py py-const docutils literal notranslate"><span class="pre">pickle.DEFAULT_PROTOCOL</span></code></a> are used
  273. to serialize values. The version of the pickle protocol can be specified
  274. with the <em>protocol</em> parameter. See the <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> documentation for a
  275. discussion of the pickle protocols.</p>
  276. <p>If the <em>writeback</em> parameter is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the object will hold a cache of all
  277. entries accessed and write them back to the <em>dict</em> at sync and close times.
  278. This allows natural operations on mutable entries, but can consume much more
  279. memory and make sync and close take a long time.</p>
  280. <p>The <em>keyencoding</em> parameter is the encoding used to encode keys before they
  281. are used with the underlying dict.</p>
  282. <p>A <a class="reference internal" href="#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a> object can also be used as a context manager, in which
  283. case it will be automatically closed when the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> block ends.</p>
  284. <div class="versionchanged">
  285. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>keyencoding</em> parameter; previously, keys were always encoded in
  286. UTF-8.</p>
  287. </div>
  288. <div class="versionchanged">
  289. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added context manager support.</p>
  290. </div>
  291. <div class="versionchanged">
  292. <p><span class="versionmodified changed">Changed in version 3.10: </span><a class="reference internal" href="pickle.html#pickle.DEFAULT_PROTOCOL" title="pickle.DEFAULT_PROTOCOL"><code class="xref py py-const docutils literal notranslate"><span class="pre">pickle.DEFAULT_PROTOCOL</span></code></a> is now used as the default pickle
  293. protocol.</p>
  294. </div>
  295. </dd></dl>
  296. <dl class="py class">
  297. <dt class="sig sig-object py" id="shelve.BsdDbShelf">
  298. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">shelve.</span></span><span class="sig-name descname"><span class="pre">BsdDbShelf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dict</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">protocol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">writeback</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">keyencoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'utf-8'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shelve.BsdDbShelf" title="Link to this definition">¶</a></dt>
  299. <dd><p>A subclass of <a class="reference internal" href="#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a> which exposes <code class="xref py py-meth docutils literal notranslate"><span class="pre">first()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code>,
  300. <code class="xref py py-meth docutils literal notranslate"><span class="pre">previous()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">last()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">set_location()</span></code> methods.
  301. These are available
  302. in the third-party <code class="xref py py-mod docutils literal notranslate"><span class="pre">bsddb</span></code> module from <a class="reference external" href="https://www.jcea.es/programacion/pybsddb.htm">pybsddb</a> but not in other database
  303. modules. The <em>dict</em> object passed to the constructor must support those
  304. methods. This is generally accomplished by calling one of
  305. <code class="xref py py-func docutils literal notranslate"><span class="pre">bsddb.hashopen()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">bsddb.btopen()</span></code> or <code class="xref py py-func docutils literal notranslate"><span class="pre">bsddb.rnopen()</span></code>. The
  306. optional <em>protocol</em>, <em>writeback</em>, and <em>keyencoding</em> parameters have the same
  307. interpretation as for the <a class="reference internal" href="#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a> class.</p>
  308. </dd></dl>
  309. <dl class="py class">
  310. <dt class="sig sig-object py" id="shelve.DbfilenameShelf">
  311. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">shelve.</span></span><span class="sig-name descname"><span class="pre">DbfilenameShelf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'c'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">protocol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">writeback</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shelve.DbfilenameShelf" title="Link to this definition">¶</a></dt>
  312. <dd><p>A subclass of <a class="reference internal" href="#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a> which accepts a <em>filename</em> instead of a dict-like
  313. object. The underlying file will be opened using <a class="reference internal" href="dbm.html#dbm.open" title="dbm.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">dbm.open()</span></code></a>. By
  314. default, the file will be created and opened for both read and write. The
  315. optional <em>flag</em> parameter has the same interpretation as for the <a class="reference internal" href="#shelve.open" title="shelve.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>
  316. function. The optional <em>protocol</em> and <em>writeback</em> parameters have the same
  317. interpretation as for the <a class="reference internal" href="#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a> class.</p>
  318. </dd></dl>
  319. </section>
  320. <section id="example">
  321. <span id="shelve-example"></span><h2>Example<a class="headerlink" href="#example" title="Link to this heading">¶</a></h2>
  322. <p>To summarize the interface (<code class="docutils literal notranslate"><span class="pre">key</span></code> is a string, <code class="docutils literal notranslate"><span class="pre">data</span></code> is an arbitrary
  323. object):</p>
  324. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">shelve</span>
  325. <span class="n">d</span> <span class="o">=</span> <span class="n">shelve</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="c1"># open -- file may get suffix added by low-level</span>
  326. <span class="c1"># library</span>
  327. <span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span> <span class="c1"># store data at key (overwrites old data if</span>
  328. <span class="c1"># using an existing key)</span>
  329. <span class="n">data</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="c1"># retrieve a COPY of data at key (raise KeyError</span>
  330. <span class="c1"># if no such key)</span>
  331. <span class="k">del</span> <span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="c1"># delete data stored at key (raises KeyError</span>
  332. <span class="c1"># if no such key)</span>
  333. <span class="n">flag</span> <span class="o">=</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">d</span> <span class="c1"># true if the key exists</span>
  334. <span class="n">klist</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="c1"># a list of all existing keys (slow!)</span>
  335. <span class="c1"># as d was opened WITHOUT writeback=True, beware:</span>
  336. <span class="n">d</span><span class="p">[</span><span class="s1">&#39;xx&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span> <span class="c1"># this works as expected, but...</span>
  337. <span class="n">d</span><span class="p">[</span><span class="s1">&#39;xx&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># *this doesn&#39;t!* -- d[&#39;xx&#39;] is STILL [0, 1, 2]!</span>
  338. <span class="c1"># having opened d without writeback=True, you need to code carefully:</span>
  339. <span class="n">temp</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;xx&#39;</span><span class="p">]</span> <span class="c1"># extracts the copy</span>
  340. <span class="n">temp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="c1"># mutates the copy</span>
  341. <span class="n">d</span><span class="p">[</span><span class="s1">&#39;xx&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span> <span class="c1"># stores the copy right back, to persist it</span>
  342. <span class="c1"># or, d=shelve.open(filename,writeback=True) would let you just code</span>
  343. <span class="c1"># d[&#39;xx&#39;].append(5) and have it work as expected, BUT it would also</span>
  344. <span class="c1"># consume more memory and make the d.close() operation slower.</span>
  345. <span class="n">d</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> <span class="c1"># close it</span>
  346. </pre></div>
  347. </div>
  348. <div class="admonition seealso">
  349. <p class="admonition-title">See also</p>
  350. <dl class="simple">
  351. <dt>Module <a class="reference internal" href="dbm.html#module-dbm" title="dbm: Interfaces to various Unix &quot;database&quot; formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a></dt><dd><p>Generic interface to <code class="docutils literal notranslate"><span class="pre">dbm</span></code>-style databases.</p>
  352. </dd>
  353. <dt>Module <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a></dt><dd><p>Object serialization used by <a class="reference internal" href="#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a>.</p>
  354. </dd>
  355. </dl>
  356. </div>
  357. </section>
  358. </section>
  359. <div class="clearer"></div>
  360. </div>
  361. </div>
  362. </div>
  363. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  364. <div class="sphinxsidebarwrapper">
  365. <div>
  366. <h3><a href="../contents.html">Table of Contents</a></h3>
  367. <ul>
  368. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code> — Python object persistence</a><ul>
  369. <li><a class="reference internal" href="#restrictions">Restrictions</a></li>
  370. <li><a class="reference internal" href="#example">Example</a></li>
  371. </ul>
  372. </li>
  373. </ul>
  374. </div>
  375. <div>
  376. <h4>Previous topic</h4>
  377. <p class="topless"><a href="copyreg.html"
  378. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copyreg</span></code> — Register <code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> support functions</a></p>
  379. </div>
  380. <div>
  381. <h4>Next topic</h4>
  382. <p class="topless"><a href="marshal.html"
  383. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code> — Internal Python object serialization</a></p>
  384. </div>
  385. <div role="note" aria-label="source link">
  386. <h3>This Page</h3>
  387. <ul class="this-page-menu">
  388. <li><a href="../bugs.html">Report a Bug</a></li>
  389. <li>
  390. <a href="https://github.com/python/cpython/blob/main/Doc/library/shelve.rst"
  391. rel="nofollow">Show Source
  392. </a>
  393. </li>
  394. </ul>
  395. </div>
  396. </div>
  397. <div id="sidebarbutton" title="Collapse sidebar">
  398. <span>«</span>
  399. </div>
  400. </div>
  401. <div class="clearer"></div>
  402. </div>
  403. <div class="related" role="navigation" aria-label="related navigation">
  404. <h3>Navigation</h3>
  405. <ul>
  406. <li class="right" style="margin-right: 10px">
  407. <a href="../genindex.html" title="General Index"
  408. >index</a></li>
  409. <li class="right" >
  410. <a href="../py-modindex.html" title="Python Module Index"
  411. >modules</a> |</li>
  412. <li class="right" >
  413. <a href="marshal.html" title="marshal — Internal Python object serialization"
  414. >next</a> |</li>
  415. <li class="right" >
  416. <a href="copyreg.html" title="copyreg — Register pickle support functions"
  417. >previous</a> |</li>
  418. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  419. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  420. <li class="switchers">
  421. <div class="language_switcher_placeholder"></div>
  422. <div class="version_switcher_placeholder"></div>
  423. </li>
  424. <li>
  425. </li>
  426. <li id="cpython-language-and-version">
  427. <a href="../index.html">3.12.3 Documentation</a> &#187;
  428. </li>
  429. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  430. <li class="nav-item nav-item-2"><a href="persistence.html" >Data Persistence</a> &#187;</li>
  431. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code> — Python object persistence</a></li>
  432. <li class="right">
  433. <div class="inline-search" role="search">
  434. <form class="inline-search" action="../search.html" method="get">
  435. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  436. <input type="submit" value="Go" />
  437. </form>
  438. </div>
  439. |
  440. </li>
  441. <li class="right">
  442. <label class="theme-selector-label">
  443. Theme
  444. <select class="theme-selector" oninput="activateTheme(this.value)">
  445. <option value="auto" selected>Auto</option>
  446. <option value="light">Light</option>
  447. <option value="dark">Dark</option>
  448. </select>
  449. </label> |</li>
  450. </ul>
  451. </div>
  452. <div class="footer">
  453. &copy;
  454. <a href="../copyright.html">
  455. Copyright
  456. </a>
  457. 2001-2024, Python Software Foundation.
  458. <br />
  459. This page is licensed under the Python Software Foundation License Version 2.
  460. <br />
  461. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  462. <br />
  463. See <a href="/license.html">History and License</a> for more information.<br />
  464. <br />
  465. The Python Software Foundation is a non-profit corporation.
  466. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  467. <br />
  468. <br />
  469. Last updated on Apr 09, 2024 (13:47 UTC).
  470. <a href="/bugs.html">Found a bug</a>?
  471. <br />
  472. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  473. </div>
  474. </body>
  475. </html>
上海开阖软件有限公司 沪ICP备12045867号-1