Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can decide and implement how any previously-unpicklable type gets pickled and unpickled: see standard library module <a href="http://docs.python.org/library/copy_reg.html" rel="nofollow noreferrer">copy_reg</a> (renamed to <code>copyreg</code> in Python 3.*).</p> <p>Essentially, you need to provide a function which, given an instance of the type, reduces it to a tuple -- with the same protocol as the <a href="http://docs.python.org/library/pickle.html#object.__reduce__" rel="nofollow noreferrer"><strong>reduce</strong></a> special method (except that the reduce special method takes no arguments, since when provided it's called directly on the object, while the function you provide will take the object as the only argument).</p> <p>Typically, the tuple you return has 2 items: a callable, and a tuple of arguments to pass to it. The callable must be registered as a "safe constructor" or equivalently have an attribute <code>__safe_for_unpickling__</code> with a true value. Those items will be pickled, and at unpickling time the callable will be called with the given arguments and must return the unpicked object.</p> <p>For example, suppose that you want to just pickle modules by name, so that unpickling them just means re-importing them (i.e. suppose for simplicity that you don't care about dynamically modified modules, nested packages, etc, just plain top-level modules). Then:</p> <pre><code>&gt;&gt;&gt; import sys, pickle, copy_reg &gt;&gt;&gt; def savemodule(module): ... return __import__, (module.__name__,) ... &gt;&gt;&gt; copy_reg.pickle(type(sys), savemodule) &gt;&gt;&gt; s = pickle.dumps(sys) &gt;&gt;&gt; s "c__builtin__\n__import__\np0\n(S'sys'\np1\ntp2\nRp3\n." &gt;&gt;&gt; z = pickle.loads(s) &gt;&gt;&gt; z &lt;module 'sys' (built-in)&gt; </code></pre> <p>I'm using the old-fashioned ASCII form of pickle so that <code>s</code>, the string containing the pickle, is easy to examine: it instructs unpickling to call the built-in import function, with the string <code>sys</code> as its sole argument. And <code>z</code> shows that this does indeed give us back the built-in <code>sys</code> module as the result of the unpickling, as desired.</p> <p>Now, you'll have to make things a bit more complex than just <code>__import__</code> (you'll have to deal with saving and restoring dynamic changes, navigate a nested namespace, etc), and thus you'll have to also call <code>copy_reg.constructor</code> (passing as argument your own function that performs this work) before you <code>copy_reg</code> the module-saving function that returns your other function (and, if in a separate run, also before you unpickle those pickles you made using said function). But I hope this simple cases helps to show that there's really nothing much to it that's at all "intrinsically" complicated!-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload