Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f334e7abeedda144e9882d5c54205bbc > files > 660

python3-docs-3.3.2-13.2.mga4.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>12.2. copyreg — Register pickle support functions &mdash; Python v3.3.2 documentation</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.3.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.3.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.3.2 documentation" href="../index.html" />
    <link rel="up" title="12. Data Persistence" href="persistence.html" />
    <link rel="next" title="12.3. shelve — Python object persistence" href="shelve.html" />
    <link rel="prev" title="12.1. pickle — Python object serialization" href="pickle.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="shelve.html" title="12.3. shelve — Python object persistence"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="pickle.html" title="12.1. pickle — Python object serialization"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">3.3.2 Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="persistence.html" accesskey="U">12. Data Persistence</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-copyreg">
<span id="copyreg-register-pickle-support-functions"></span><h1>12.2. <a class="reference internal" href="#module-copyreg" title="copyreg: Register pickle support functions."><tt class="xref py py-mod docutils literal"><span class="pre">copyreg</span></tt></a> &#8212; Register <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> support functions<a class="headerlink" href="#module-copyreg" title="Permalink to this headline">¶</a></h1>
<p id="index-0">The <a class="reference internal" href="#module-copyreg" title="copyreg: Register pickle support functions."><tt class="xref py py-mod docutils literal"><span class="pre">copyreg</span></tt></a> module offers a way to define fuctions used while pickling
specific objects.  The <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> and <a class="reference internal" href="copy.html#module-copy" title="copy: Shallow and deep copy operations."><tt class="xref py py-mod docutils literal"><span class="pre">copy</span></tt></a> modules use those functions
when pickling/copying those objects.  The module provides configuration
information about object constructors which are not classes.
Such constructors may be factory functions or class instances.</p>
<dl class="function">
<dt id="copyreg.constructor">
<tt class="descclassname">copyreg.</tt><tt class="descname">constructor</tt><big>(</big><em>object</em><big>)</big><a class="headerlink" href="#copyreg.constructor" title="Permalink to this definition">¶</a></dt>
<dd><p>Declares <em>object</em> to be a valid constructor.  If <em>object</em> is not callable (and
hence not valid as a constructor), raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="copyreg.pickle">
<tt class="descclassname">copyreg.</tt><tt class="descname">pickle</tt><big>(</big><em>type</em>, <em>function</em>, <em>constructor=None</em><big>)</big><a class="headerlink" href="#copyreg.pickle" title="Permalink to this definition">¶</a></dt>
<dd><p>Declares that <em>function</em> should be used as a &#8220;reduction&#8221; function for objects
of type <em>type</em>.  <em>function</em> should return either a string or a tuple
containing two or three elements.</p>
<p>The optional <em>constructor</em> parameter, if provided, is a callable object which
can be used to reconstruct the object when called with the tuple of arguments
returned by <em>function</em> at pickling time.  <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> will be raised if
<em>object</em> is a class or <em>constructor</em> is not callable.</p>
<p>See the <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module for more details on the interface
expected of <em>function</em> and <em>constructor</em>.  Note that the
<a class="reference internal" href="pickle.html#pickle.Pickler.dispatch_table" title="pickle.Pickler.dispatch_table"><tt class="xref py py-attr docutils literal"><span class="pre">dispatch_table</span></tt></a> attribute of a pickler
object or subclass of <a class="reference internal" href="pickle.html#pickle.Pickler" title="pickle.Pickler"><tt class="xref py py-class docutils literal"><span class="pre">pickle.Pickler</span></tt></a> can also be used for
declaring reduction functions.</p>
</dd></dl>

<div class="section" id="example">
<h2>12.2.1. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<p>The example below would like to show how to register a pickle function and how
it will be used:</p>
<div class="highlight-python3"><div class="highlight"><pre><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>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">):</span>
<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>
<span class="gp">...</span>
<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>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="s">&quot;pickling a C instance...&quot;</span><span class="p">)</span>
<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>
<span class="gp">...</span>
<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>
<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>
<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>
<span class="go">pickling a C instance...</span>
<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>
<span class="go">pickling a C instance...</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">12.2. <tt class="docutils literal"><span class="pre">copyreg</span></tt> &#8212; Register <tt class="docutils literal"><span class="pre">pickle</span></tt> support functions</a><ul>
<li><a class="reference internal" href="#example">12.2.1. Example</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="pickle.html"
                        title="previous chapter">12.1. <tt class="docutils literal"><span class="pre">pickle</span></tt> &#8212; Python object serialization</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="shelve.html"
                        title="next chapter">12.3. <tt class="docutils literal"><span class="pre">shelve</span></tt> &#8212; Python object persistence</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/copyreg.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="shelve.html" title="12.3. shelve — Python object persistence"
             >next</a> |</li>
        <li class="right" >
          <a href="pickle.html" title="12.1. pickle — Python object serialization"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">3.3.2 Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="persistence.html" >12. Data Persistence</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2013, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on May 15, 2013.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>