summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fcopyreg.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /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.html27
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">&gt;&gt;&gt; import copyreg, copy, pickle
+&gt;&gt;&gt; class C:
+... def __init__(self, a):
+... self.a = a
+...
+&gt;&gt;&gt; def pickle_c(c):
+... print("pickling a C instance...")
+... return C, (c.a,)
+...
+&gt;&gt;&gt; copyreg.pickle(C, pickle_c)
+&gt;&gt;&gt; c = C(1)
+&gt;&gt;&gt; d = copy.copy(c)
+pickling a C instance...
+&gt;&gt;&gt; p = pickle.dumps(c)
+pickling a C instance...
+</pre> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;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>