| 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="node64.html" /> |
| 13 | <link rel="prev" href="module-pickle.html" /> |
| 14 | <link rel="parent" href="module-pickle.html" /> |
| 15 | <link rel="next" href="node64.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.14.1 Relationship to other Python modules</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 pickle " |
| 25 | href="module-pickle.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 pickle " |
| 28 | href="module-pickle.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.2 Data stream format" |
| 31 | href="node64.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="module-pickle.html">3.14 pickle </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-pickle.html">3.14 pickle </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node64.html">3.14.2 Data stream format</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0051410000000000000000"> |
| 56 | 3.14.1 Relationship to other Python modules</A> |
| 57 | </H2> |
| 58 | |
| 59 | <P> |
| 60 | The <tt class="module">pickle</tt> module has an optimized cousin called the |
| 61 | <tt class="module">cPickle</tt> module. As its name implies, <tt class="module">cPickle</tt> is |
| 62 | written in C, so it can be up to 1000 times faster than |
| 63 | <tt class="module">pickle</tt>. However it does not support subclassing of the |
| 64 | <tt class="function">Pickler()</tt> and <tt class="function">Unpickler()</tt> classes, because in |
| 65 | <tt class="module">cPickle</tt> these are functions, not classes. Most applications |
| 66 | have no need for this functionality, and can benefit from the improved |
| 67 | performance of <tt class="module">cPickle</tt>. Other than that, the interfaces of |
| 68 | the two modules are nearly identical; the common interface is |
| 69 | described in this manual and differences are pointed out where |
| 70 | necessary. In the following discussions, we use the term ``pickle'' |
| 71 | to collectively describe the <tt class="module">pickle</tt> and |
| 72 | <tt class="module">cPickle</tt> modules. |
| 73 | |
| 74 | <P> |
| 75 | The data streams the two modules produce are guaranteed to be |
| 76 | interchangeable. |
| 77 | |
| 78 | <P> |
| 79 | Python has a more primitive serialization module called |
| 80 | <tt class="module"><a href="module-marshal.html">marshal</a></tt>, but in general |
| 81 | <tt class="module">pickle</tt> should always be the preferred way to serialize Python |
| 82 | objects. <tt class="module">marshal</tt> exists primarily to support Python's |
| 83 | <span class="file">.pyc</span> files. |
| 84 | |
| 85 | <P> |
| 86 | The <tt class="module">pickle</tt> module differs from <tt class="module"><a href="module-marshal.html">marshal</a></tt> several |
| 87 | significant ways: |
| 88 | |
| 89 | <P> |
| 90 | |
| 91 | <UL> |
| 92 | <LI>The <tt class="module">pickle</tt> module keeps track of the objects it has |
| 93 | already serialized, so that later references to the same object |
| 94 | won't be serialized again. <tt class="module">marshal</tt> doesn't do this. |
| 95 | |
| 96 | <P> |
| 97 | This has implications both for recursive objects and object |
| 98 | sharing. Recursive objects are objects that contain references |
| 99 | to themselves. These are not handled by marshal, and in fact, |
| 100 | attempting to marshal recursive objects will crash your Python |
| 101 | interpreter. Object sharing happens when there are multiple |
| 102 | references to the same object in different places in the object |
| 103 | hierarchy being serialized. <tt class="module">pickle</tt> stores such objects |
| 104 | only once, and ensures that all other references point to the |
| 105 | master copy. Shared objects remain shared, which can be very |
| 106 | important for mutable objects. |
| 107 | |
| 108 | <P> |
| 109 | </LI> |
| 110 | <LI><tt class="module">marshal</tt> cannot be used to serialize user-defined |
| 111 | classes and their instances. <tt class="module">pickle</tt> can save and |
| 112 | restore class instances transparently, however the class |
| 113 | definition must be importable and live in the same module as |
| 114 | when the object was stored. |
| 115 | |
| 116 | <P> |
| 117 | </LI> |
| 118 | <LI>The <tt class="module">marshal</tt> serialization format is not guaranteed to |
| 119 | be portable across Python versions. Because its primary job in |
| 120 | life is to support <span class="file">.pyc</span> files, the Python implementers |
| 121 | reserve the right to change the serialization format in |
| 122 | non-backwards compatible ways should the need arise. The |
| 123 | <tt class="module">pickle</tt> serialization format is guaranteed to be |
| 124 | backwards compatible across Python releases. |
| 125 | |
| 126 | <P> |
| 127 | </LI> |
| 128 | </UL> |
| 129 | |
| 130 | <P> |
| 131 | <div class="warning"><b class="label">Warning:</b> |
| 132 | |
| 133 | The <tt class="module">pickle</tt> module is not intended to be secure against |
| 134 | erroneous or maliciously constructed data. Never unpickle data |
| 135 | received from an untrusted or unauthenticated source. |
| 136 | </div> |
| 137 | |
| 138 | <P> |
| 139 | Note that serialization is a more primitive notion than persistence; |
| 140 | although |
| 141 | <tt class="module">pickle</tt> reads and writes file objects, it does not handle the |
| 142 | issue of naming persistent objects, nor the (even more complicated) |
| 143 | issue of concurrent access to persistent objects. The <tt class="module">pickle</tt> |
| 144 | module can transform a complex object into a byte stream and it can |
| 145 | transform the byte stream into an object with the same internal |
| 146 | structure. Perhaps the most obvious thing to do with these byte |
| 147 | streams is to write them onto a file, but it is also conceivable to |
| 148 | send them across a network or store them in a database. The module |
| 149 | <tt class="module"><a href="module-shelve.html">shelve</a></tt> provides a simple interface |
| 150 | to pickle and unpickle objects on DBM-style database files. |
| 151 | |
| 152 | <P> |
| 153 | |
| 154 | <DIV CLASS="navigation"> |
| 155 | <div class='online-navigation'> |
| 156 | <p></p><hr /> |
| 157 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 158 | <tr> |
| 159 | <td class='online-navigation'><a rel="prev" title="3.14 pickle " |
| 160 | href="module-pickle.html"><img src='../icons/previous.png' |
| 161 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 162 | <td class='online-navigation'><a rel="parent" title="3.14 pickle " |
| 163 | href="module-pickle.html"><img src='../icons/up.png' |
| 164 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 165 | <td class='online-navigation'><a rel="next" title="3.14.2 Data stream format" |
| 166 | href="node64.html"><img src='../icons/next.png' |
| 167 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 168 | <td align="center" width="100%">Python Library Reference</td> |
| 169 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 170 | href="contents.html"><img src='../icons/contents.png' |
| 171 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 172 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 173 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 174 | <td class='online-navigation'><a rel="index" title="Index" |
| 175 | href="genindex.html"><img src='../icons/index.png' |
| 176 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 177 | </tr></table> |
| 178 | <div class='online-navigation'> |
| 179 | <b class="navlabel">Previous:</b> |
| 180 | <a class="sectref" rel="prev" href="module-pickle.html">3.14 pickle </A> |
| 181 | <b class="navlabel">Up:</b> |
| 182 | <a class="sectref" rel="parent" href="module-pickle.html">3.14 pickle </A> |
| 183 | <b class="navlabel">Next:</b> |
| 184 | <a class="sectref" rel="next" href="node64.html">3.14.2 Data stream format</A> |
| 185 | </div> |
| 186 | </div> |
| 187 | <hr /> |
| 188 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 189 | </DIV> |
| 190 | <!--End of Navigation Panel--> |
| 191 | <ADDRESS> |
| 192 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 193 | </ADDRESS> |
| 194 | </BODY> |
| 195 | </HTML> |