| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="node70.html" /> |
| 13 | <link rel="prev" href="pickle-inst.html" /> |
| 14 | <link rel="parent" href="pickle-protocol.html" /> |
| 15 | <link rel="next" href="node70.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.14.5.2 Pickling and unpickling extension types</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="3.14.5.1 Pickling and unpickling" |
| 25 | href="pickle-inst.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="3.14.5 The pickle protocol" |
| 28 | href="pickle-protocol.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="3.14.5.3 Pickling and unpickling" |
| 31 | href="node70.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="pickle-inst.html">3.14.5.1 Pickling and unpickling</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="pickle-protocol.html">3.14.5 The pickle protocol</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node70.html">3.14.5.3 Pickling and unpickling</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H3><A NAME="SECTION0051452000000000000000"> |
| 56 | 3.14.5.2 Pickling and unpickling extension types</A> |
| 57 | </H3> |
| 58 | |
| 59 | <P> |
| 60 | When the <tt class="class">Pickler</tt> encounters an object of a type it knows |
| 61 | nothing about -- such as an extension type -- it looks in two places |
| 62 | for a hint of how to pickle it. One alternative is for the object to |
| 63 | implement a <tt class="method">__reduce__()</tt> method. If provided, at pickling |
| 64 | time <tt class="method">__reduce__()</tt> will be called with no arguments, and it |
| 65 | must return either a string or a tuple. |
| 66 | |
| 67 | <P> |
| 68 | If a string is returned, it names a global variable whose contents are |
| 69 | pickled as normal. The string returned by <tt class="method">__reduce__</tt> should |
| 70 | be the object's local name relative to its module; the pickle module |
| 71 | searches the module namespace to determine the object's module. |
| 72 | |
| 73 | <P> |
| 74 | When a tuple is returned, it must be between two and five elements |
| 75 | long. Optional elements can either be omitted, or <code>None</code> can be provided |
| 76 | as their value. The semantics of each element are: |
| 77 | |
| 78 | <P> |
| 79 | |
| 80 | <UL> |
| 81 | <LI>A callable object that will be called to create the initial |
| 82 | version of the object. The next element of the tuple will provide |
| 83 | arguments for this callable, and later elements provide additional |
| 84 | state information that will subsequently be used to fully reconstruct |
| 85 | the pickled date. |
| 86 | |
| 87 | <P> |
| 88 | In the unpickling environment this object must be either a class, a |
| 89 | callable registered as a ``safe constructor'' (see below), or it must |
| 90 | have an attribute <tt class="member">__safe_for_unpickling__</tt> with a true value. |
| 91 | Otherwise, an <tt class="exception">UnpicklingError</tt> will be raised in the |
| 92 | unpickling environment. Note that as usual, the callable itself is |
| 93 | pickled by name. |
| 94 | |
| 95 | <P> |
| 96 | </LI> |
| 97 | <LI>A tuple of arguments for the callable object, or <code>None</code>. |
| 98 | <div class="versionnote"><b>Deprecated since release 2.3.</b> |
| 99 | If this item is <code>None</code>, then instead of calling |
| 100 | the callable directly, its <tt class="method">__basicnew__()</tt> method is called |
| 101 | without arguments; this method should also return the unpickled |
| 102 | object. Providing <code>None</code> is deprecated, however; return a |
| 103 | tuple of arguments instead.</div><p></p> |
| 104 | |
| 105 | <P> |
| 106 | </LI> |
| 107 | <LI>Optionally, the object's state, which will be passed to |
| 108 | the object's <tt class="method">__setstate__()</tt> method as described in |
| 109 | section <A href="pickle-inst.html#pickle-inst">3.14.5</A>. If the object has no |
| 110 | <tt class="method">__setstate__()</tt> method, then, as above, the value must |
| 111 | be a dictionary and it will be added to the object's |
| 112 | <tt class="member">__dict__</tt>. |
| 113 | |
| 114 | <P> |
| 115 | </LI> |
| 116 | <LI>Optionally, an iterator (and not a sequence) yielding successive |
| 117 | list items. These list items will be pickled, and appended to the |
| 118 | object using either <code>obj.append(<var>item</var>)</code> or |
| 119 | <code>obj.extend(<var>list_of_items</var>)</code>. This is primarily used for |
| 120 | list subclasses, but may be used by other classes as long as they have |
| 121 | <tt class="method">append()</tt> and <tt class="method">extend()</tt> methods with the appropriate |
| 122 | signature. (Whether <tt class="method">append()</tt> or <tt class="method">extend()</tt> is used |
| 123 | depends on which pickle protocol version is used as well as the number |
| 124 | of items to append, so both must be supported.) |
| 125 | |
| 126 | <P> |
| 127 | </LI> |
| 128 | <LI>Optionally, an iterator (not a sequence) |
| 129 | yielding successive dictionary items, which should be tuples of the |
| 130 | form <code>(<var>key</var>, <var>value</var>)</code>. These items will be pickled |
| 131 | and stored to the object using <code>obj[<var>key</var>] = <var>value</var></code>. |
| 132 | This is primarily used for dictionary subclasses, but may be used by |
| 133 | other classes as long as they implement <tt class="method">__setitem__</tt>. |
| 134 | |
| 135 | <P> |
| 136 | </LI> |
| 137 | </UL> |
| 138 | |
| 139 | <P> |
| 140 | It is sometimes useful to know the protocol version when implementing |
| 141 | <tt class="method">__reduce__</tt>. This can be done by implementing a method named |
| 142 | <tt class="method">__reduce_ex__</tt> instead of <tt class="method">__reduce__</tt>. |
| 143 | <tt class="method">__reduce_ex__</tt>, when it exists, is called in preference over |
| 144 | <tt class="method">__reduce__</tt> (you may still provide <tt class="method">__reduce__</tt> for |
| 145 | backwards compatibility). The <tt class="method">__reduce_ex__</tt> method will be |
| 146 | called with a single integer argument, the protocol version. |
| 147 | |
| 148 | <P> |
| 149 | The <tt class="class">object</tt> class implements both <tt class="method">__reduce__</tt> and |
| 150 | <tt class="method">__reduce_ex__</tt>; however, if a subclass overrides |
| 151 | <tt class="method">__reduce__</tt> but not <tt class="method">__reduce_ex__</tt>, the |
| 152 | <tt class="method">__reduce_ex__</tt> implementation detects this and calls |
| 153 | <tt class="method">__reduce__</tt>. |
| 154 | |
| 155 | <P> |
| 156 | An alternative to implementing a <tt class="method">__reduce__()</tt> method on the |
| 157 | object to be pickled, is to register the callable with the |
| 158 | <tt class="module"><a href="module-copyreg.html">copy_reg</a></tt> module. This module provides a way |
| 159 | for programs to register ``reduction functions'' and constructors for |
| 160 | user-defined types. Reduction functions have the same semantics and |
| 161 | interface as the <tt class="method">__reduce__()</tt> method described above, except |
| 162 | that they are called with a single argument, the object to be pickled. |
| 163 | |
| 164 | <P> |
| 165 | The registered constructor is deemed a ``safe constructor'' for purposes |
| 166 | of unpickling as described above. |
| 167 | |
| 168 | <P> |
| 169 | |
| 170 | <DIV CLASS="navigation"> |
| 171 | <div class='online-navigation'> |
| 172 | <p></p><hr /> |
| 173 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 174 | <tr> |
| 175 | <td class='online-navigation'><a rel="prev" title="3.14.5.1 Pickling and unpickling" |
| 176 | href="pickle-inst.html"><img src='../icons/previous.png' |
| 177 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 178 | <td class='online-navigation'><a rel="parent" title="3.14.5 The pickle protocol" |
| 179 | href="pickle-protocol.html"><img src='../icons/up.png' |
| 180 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 181 | <td class='online-navigation'><a rel="next" title="3.14.5.3 Pickling and unpickling" |
| 182 | href="node70.html"><img src='../icons/next.png' |
| 183 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 184 | <td align="center" width="100%">Python Library Reference</td> |
| 185 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 186 | href="contents.html"><img src='../icons/contents.png' |
| 187 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 188 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 189 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 190 | <td class='online-navigation'><a rel="index" title="Index" |
| 191 | href="genindex.html"><img src='../icons/index.png' |
| 192 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 193 | </tr></table> |
| 194 | <div class='online-navigation'> |
| 195 | <b class="navlabel">Previous:</b> |
| 196 | <a class="sectref" rel="prev" href="pickle-inst.html">3.14.5.1 Pickling and unpickling</A> |
| 197 | <b class="navlabel">Up:</b> |
| 198 | <a class="sectref" rel="parent" href="pickle-protocol.html">3.14.5 The pickle protocol</A> |
| 199 | <b class="navlabel">Next:</b> |
| 200 | <a class="sectref" rel="next" href="node70.html">3.14.5.3 Pickling and unpickling</A> |
| 201 | </div> |
| 202 | </div> |
| 203 | <hr /> |
| 204 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 205 | </DIV> |
| 206 | <!--End of Navigation Panel--> |
| 207 | <ADDRESS> |
| 208 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 209 | </ADDRESS> |
| 210 | </BODY> |
| 211 | </HTML> |