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.

380 line
21KB

  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="copyreg — Register pickle support functions" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/copyreg.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/copyreg.py The copyreg module offers a way to define functions used while pickling specific objects. The pickle and copy modules use those functions when pickling/copying those obj..." />
  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/copyreg.py The copyreg module offers a way to define functions used while pickling specific objects. The pickle and copy modules use those functions when pickling/copying those obj..." />
  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>copyreg — Register pickle support functions &#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="shelve — Python object persistence" href="shelve.html" />
  33. <link rel="prev" title="pickle — Python object serialization" href="pickle.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/copyreg.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">copyreg</span></code> — Register <code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> support functions</a><ul>
  86. <li><a class="reference internal" href="#example">Example</a></li>
  87. </ul>
  88. </li>
  89. </ul>
  90. </div>
  91. <div>
  92. <h4>Previous topic</h4>
  93. <p class="topless"><a href="pickle.html"
  94. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> — Python object serialization</a></p>
  95. </div>
  96. <div>
  97. <h4>Next topic</h4>
  98. <p class="topless"><a href="shelve.html"
  99. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code> — Python object persistence</a></p>
  100. </div>
  101. <div role="note" aria-label="source link">
  102. <h3>This Page</h3>
  103. <ul class="this-page-menu">
  104. <li><a href="../bugs.html">Report a Bug</a></li>
  105. <li>
  106. <a href="https://github.com/python/cpython/blob/main/Doc/library/copyreg.rst"
  107. rel="nofollow">Show Source
  108. </a>
  109. </li>
  110. </ul>
  111. </div>
  112. </nav>
  113. </div>
  114. </div>
  115. <div class="related" role="navigation" aria-label="related navigation">
  116. <h3>Navigation</h3>
  117. <ul>
  118. <li class="right" style="margin-right: 10px">
  119. <a href="../genindex.html" title="General Index"
  120. accesskey="I">index</a></li>
  121. <li class="right" >
  122. <a href="../py-modindex.html" title="Python Module Index"
  123. >modules</a> |</li>
  124. <li class="right" >
  125. <a href="shelve.html" title="shelve — Python object persistence"
  126. accesskey="N">next</a> |</li>
  127. <li class="right" >
  128. <a href="pickle.html" title="pickle — Python object serialization"
  129. accesskey="P">previous</a> |</li>
  130. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  131. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  132. <li class="switchers">
  133. <div class="language_switcher_placeholder"></div>
  134. <div class="version_switcher_placeholder"></div>
  135. </li>
  136. <li>
  137. </li>
  138. <li id="cpython-language-and-version">
  139. <a href="../index.html">3.12.3 Documentation</a> &#187;
  140. </li>
  141. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  142. <li class="nav-item nav-item-2"><a href="persistence.html" accesskey="U">Data Persistence</a> &#187;</li>
  143. <li class="nav-item nav-item-this"><a href=""><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></li>
  144. <li class="right">
  145. <div class="inline-search" role="search">
  146. <form class="inline-search" action="../search.html" method="get">
  147. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  148. <input type="submit" value="Go" />
  149. </form>
  150. </div>
  151. |
  152. </li>
  153. <li class="right">
  154. <label class="theme-selector-label">
  155. Theme
  156. <select class="theme-selector" oninput="activateTheme(this.value)">
  157. <option value="auto" selected>Auto</option>
  158. <option value="light">Light</option>
  159. <option value="dark">Dark</option>
  160. </select>
  161. </label> |</li>
  162. </ul>
  163. </div>
  164. <div class="document">
  165. <div class="documentwrapper">
  166. <div class="bodywrapper">
  167. <div class="body" role="main">
  168. <section id="module-copyreg">
  169. <span id="copyreg-register-pickle-support-functions"></span><h1><a class="reference internal" href="#module-copyreg" title="copyreg: Register pickle support functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">copyreg</span></code></a> — Register <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> support functions<a class="headerlink" href="#module-copyreg" title="Link to this heading">¶</a></h1>
  170. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/copyreg.py">Lib/copyreg.py</a></p>
  171. <hr class="docutils" id="index-0" />
  172. <p>The <a class="reference internal" href="#module-copyreg" title="copyreg: Register pickle support functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">copyreg</span></code></a> module offers a way to define functions used while pickling
  173. specific objects. 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> and <a class="reference internal" href="copy.html#module-copy" title="copy: Shallow and deep copy operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">copy</span></code></a> modules use those functions
  174. when pickling/copying those objects. The module provides configuration
  175. information about object constructors which are not classes.
  176. Such constructors may be factory functions or class instances.</p>
  177. <dl class="py function">
  178. <dt class="sig sig-object py" id="copyreg.constructor">
  179. <span class="sig-prename descclassname"><span class="pre">copyreg.</span></span><span class="sig-name descname"><span class="pre">constructor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#copyreg.constructor" title="Link to this definition">¶</a></dt>
  180. <dd><p>Declares <em>object</em> to be a valid constructor. If <em>object</em> is not callable (and
  181. hence not valid as a constructor), raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p>
  182. </dd></dl>
  183. <dl class="py function">
  184. <dt class="sig sig-object py" id="copyreg.pickle">
  185. <span class="sig-prename descclassname"><span class="pre">copyreg.</span></span><span class="sig-name descname"><span class="pre">pickle</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">type</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">function</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">constructor_ob</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#copyreg.pickle" title="Link to this definition">¶</a></dt>
  186. <dd><p>Declares that <em>function</em> should be used as a “reduction” function for objects
  187. of type <em>type</em>. <em>function</em> must return either a string or a tuple
  188. containing between two and six elements. See the <a class="reference internal" href="pickle.html#pickle.Pickler.dispatch_table" title="pickle.Pickler.dispatch_table"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dispatch_table</span></code></a>
  189. for more details on the interface of <em>function</em>.</p>
  190. <p>The <em>constructor_ob</em> parameter is a legacy feature and is now ignored, but if
  191. passed it must be a callable.</p>
  192. <p>Note that the <a class="reference internal" href="pickle.html#pickle.Pickler.dispatch_table" title="pickle.Pickler.dispatch_table"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dispatch_table</span></code></a> attribute of a pickler
  193. object or subclass of <a class="reference internal" href="pickle.html#pickle.Pickler" title="pickle.Pickler"><code class="xref py py-class docutils literal notranslate"><span class="pre">pickle.Pickler</span></code></a> can also be used for
  194. declaring reduction functions.</p>
  195. </dd></dl>
  196. <section id="example">
  197. <h2>Example<a class="headerlink" href="#example" title="Link to this heading">¶</a></h2>
  198. <p>The example below would like to show how to register a pickle function and how
  199. it will be used:</p>
  200. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">copyreg</span><span class="o">,</span> <span class="nn">copy</span><span class="o">,</span> <span class="nn">pickle</span>
  201. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
  202. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">):</span>
  203. <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="n">a</span>
  204. <span class="gp">...</span>
  205. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">pickle_c</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
  206. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;pickling a C instance...&quot;</span><span class="p">)</span>
  207. <span class="gp">... </span> <span class="k">return</span> <span class="n">C</span><span class="p">,</span> <span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">a</span><span class="p">,)</span>
  208. <span class="gp">...</span>
  209. <span class="gp">&gt;&gt;&gt; </span><span class="n">copyreg</span><span class="o">.</span><span class="n">pickle</span><span class="p">(</span><span class="n">C</span><span class="p">,</span> <span class="n">pickle_c</span><span class="p">)</span>
  210. <span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  211. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
  212. <span class="go">pickling a C instance...</span>
  213. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
  214. <span class="go">pickling a C instance...</span>
  215. </pre></div>
  216. </div>
  217. </section>
  218. </section>
  219. <div class="clearer"></div>
  220. </div>
  221. </div>
  222. </div>
  223. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  224. <div class="sphinxsidebarwrapper">
  225. <div>
  226. <h3><a href="../contents.html">Table of Contents</a></h3>
  227. <ul>
  228. <li><a class="reference internal" href="#"><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><ul>
  229. <li><a class="reference internal" href="#example">Example</a></li>
  230. </ul>
  231. </li>
  232. </ul>
  233. </div>
  234. <div>
  235. <h4>Previous topic</h4>
  236. <p class="topless"><a href="pickle.html"
  237. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> — Python object serialization</a></p>
  238. </div>
  239. <div>
  240. <h4>Next topic</h4>
  241. <p class="topless"><a href="shelve.html"
  242. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code> — Python object persistence</a></p>
  243. </div>
  244. <div role="note" aria-label="source link">
  245. <h3>This Page</h3>
  246. <ul class="this-page-menu">
  247. <li><a href="../bugs.html">Report a Bug</a></li>
  248. <li>
  249. <a href="https://github.com/python/cpython/blob/main/Doc/library/copyreg.rst"
  250. rel="nofollow">Show Source
  251. </a>
  252. </li>
  253. </ul>
  254. </div>
  255. </div>
  256. <div id="sidebarbutton" title="Collapse sidebar">
  257. <span>«</span>
  258. </div>
  259. </div>
  260. <div class="clearer"></div>
  261. </div>
  262. <div class="related" role="navigation" aria-label="related navigation">
  263. <h3>Navigation</h3>
  264. <ul>
  265. <li class="right" style="margin-right: 10px">
  266. <a href="../genindex.html" title="General Index"
  267. >index</a></li>
  268. <li class="right" >
  269. <a href="../py-modindex.html" title="Python Module Index"
  270. >modules</a> |</li>
  271. <li class="right" >
  272. <a href="shelve.html" title="shelve — Python object persistence"
  273. >next</a> |</li>
  274. <li class="right" >
  275. <a href="pickle.html" title="pickle — Python object serialization"
  276. >previous</a> |</li>
  277. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  278. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  279. <li class="switchers">
  280. <div class="language_switcher_placeholder"></div>
  281. <div class="version_switcher_placeholder"></div>
  282. </li>
  283. <li>
  284. </li>
  285. <li id="cpython-language-and-version">
  286. <a href="../index.html">3.12.3 Documentation</a> &#187;
  287. </li>
  288. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  289. <li class="nav-item nav-item-2"><a href="persistence.html" >Data Persistence</a> &#187;</li>
  290. <li class="nav-item nav-item-this"><a href=""><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></li>
  291. <li class="right">
  292. <div class="inline-search" role="search">
  293. <form class="inline-search" action="../search.html" method="get">
  294. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  295. <input type="submit" value="Go" />
  296. </form>
  297. </div>
  298. |
  299. </li>
  300. <li class="right">
  301. <label class="theme-selector-label">
  302. Theme
  303. <select class="theme-selector" oninput="activateTheme(this.value)">
  304. <option value="auto" selected>Auto</option>
  305. <option value="light">Light</option>
  306. <option value="dark">Dark</option>
  307. </select>
  308. </label> |</li>
  309. </ul>
  310. </div>
  311. <div class="footer">
  312. &copy;
  313. <a href="../copyright.html">
  314. Copyright
  315. </a>
  316. 2001-2024, Python Software Foundation.
  317. <br />
  318. This page is licensed under the Python Software Foundation License Version 2.
  319. <br />
  320. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  321. <br />
  322. See <a href="/license.html">History and License</a> for more information.<br />
  323. <br />
  324. The Python Software Foundation is a non-profit corporation.
  325. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  326. <br />
  327. <br />
  328. Last updated on Apr 09, 2024 (13:47 UTC).
  329. <a href="/bugs.html">Found a bug</a>?
  330. <br />
  331. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  332. </div>
  333. </body>
  334. </html>
上海开阖软件有限公司 沪ICP备12045867号-1