diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Fcopyreg.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Fcopyreg.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Fcopyreg.html | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fcopyreg.html b/devdocs/python~3.12/library%2Fcopyreg.html new file mode 100644 index 00000000..42c6898c --- /dev/null +++ b/devdocs/python~3.12/library%2Fcopyreg.html @@ -0,0 +1,27 @@ + <span id="copyreg-register-pickle-support-functions"></span><h1>copyreg — Register pickle support functions</h1> <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> <p>The <a class="reference internal" href="#module-copyreg" title="copyreg: Register pickle support functions."><code>copyreg</code></a> module offers a way to define functions used while pickling specific objects. The <a class="reference internal" href="pickle#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code>pickle</code></a> and <a class="reference internal" href="copy#module-copy" title="copy: Shallow and deep copy operations."><code>copy</code></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="py function"> <dt class="sig sig-object py" id="copyreg.constructor"> +<code>copyreg.constructor(object)</code> </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#TypeError" title="TypeError"><code>TypeError</code></a>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="copyreg.pickle"> +<code>copyreg.pickle(type, function, constructor_ob=None)</code> </dt> <dd> +<p>Declares that <em>function</em> should be used as a “reduction” function for objects of type <em>type</em>. <em>function</em> must return either a string or a tuple containing between two and six elements. See the <a class="reference internal" href="pickle#pickle.Pickler.dispatch_table" title="pickle.Pickler.dispatch_table"><code>dispatch_table</code></a> for more details on the interface of <em>function</em>.</p> <p>The <em>constructor_ob</em> parameter is a legacy feature and is now ignored, but if passed it must be a callable.</p> <p>Note that the <a class="reference internal" href="pickle#pickle.Pickler.dispatch_table" title="pickle.Pickler.dispatch_table"><code>dispatch_table</code></a> attribute of a pickler object or subclass of <a class="reference internal" href="pickle#pickle.Pickler" title="pickle.Pickler"><code>pickle.Pickler</code></a> can also be used for declaring reduction functions.</p> </dd> +</dl> <section id="example"> <h2>Example</h2> <p>The example below would like to show how to register a pickle function and how it will be used:</p> <pre data-language="python">>>> import copyreg, copy, pickle +>>> class C: +... def __init__(self, a): +... self.a = a +... +>>> def pickle_c(c): +... print("pickling a C instance...") +... return C, (c.a,) +... +>>> copyreg.pickle(C, pickle_c) +>>> c = C(1) +>>> d = copy.copy(c) +pickling a C instance... +>>> p = pickle.dumps(c) +pickling a C instance... +</pre> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/copyreg.html" class="_attribution-link">https://docs.python.org/3.12/library/copyreg.html</a> + </p> +</div> |
