Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 1 | \section{Built-in module \sectcode{pickle}} |
| 2 | \stmodindex{pickle} |
| 3 | \index{persistency} |
| 4 | \indexii{persistent}{objects} |
| 5 | \indexii{serializing}{objects} |
| 6 | \indexii{marshalling}{objects} |
| 7 | \indexii{flattening}{objects} |
| 8 | \indexii{pickling}{objects} |
| 9 | |
| 10 | The \code{pickle} module implements a basic but powerful algorithm for |
| 11 | ``pickling'' (a.k.a. serializing, marshalling or flattening) nearly |
| 12 | arbitrary Python objects. This is a more primitive notion than |
| 13 | persistency --- although \code{pickle} reads and writes file objects, |
| 14 | it does not handle the issue of naming persistent objects, nor the |
| 15 | (even more complicated) area of concurrent access to persistent |
| 16 | objects. The \code{pickle} module can transform a complex object into |
| 17 | a byte stream and it can transform the byte stream into an object with |
| 18 | the same internal structure. The most obvious thing to do with these |
| 19 | byte streams is to write them onto a file, but it is also conceivable |
| 20 | to send them across a network or store them in a database. The module |
| 21 | \code{shelve} provides a simple interface to pickle and unpickle |
| 22 | objects on ``dbm''-style database files. |
| 23 | \stmodindex{shelve} |
| 24 | |
| 25 | Unlike the built-in module \code{marshal}, \code{pickle} handles the |
| 26 | following correctly: |
| 27 | \stmodindex{marshal} |
| 28 | |
| 29 | \begin{itemize} |
| 30 | |
| 31 | \item recursive objects |
| 32 | |
| 33 | \item pointer sharing |
| 34 | |
| 35 | \item instances uf user-defined classes |
| 36 | |
| 37 | \end{itemize} |
| 38 | |
| 39 | The data format used by \code{pickle} is Python-specific. This has |
| 40 | the advantage that there are no restrictions imposed by external |
| 41 | standards such as CORBA (which probably can't represent pointer |
| 42 | sharing or recursive objects); however it means that non-Python |
| 43 | programs may not be able to reconstruct pickled Python objects. |
| 44 | |
| 45 | The \code{pickle} data format uses a printable ASCII representation. |
| 46 | This is slightly more voluminous than a binary representation. |
| 47 | However, small integers actually take {\em less} space when |
| 48 | represented as minimal-size decimal strings than when represented as |
| 49 | 32-bit binary numbers, and strings are only much longer if they |
| 50 | contain many control characters or 8-bit characters. The big |
| 51 | advantage of using printable ASCII (and of some other characteristics |
| 52 | of \code{pickle}'s representation) is that for debugging or recovery |
| 53 | purposes it is possible for a human to read the pickled file with a |
| 54 | standard text editor. (I could have gone a step further and used a |
| 55 | notation like S-expressions, but the parser would have been |
| 56 | considerably more complicated and slower, and the files would probably |
| 57 | have become much larger.) |
| 58 | |
| 59 | The \code{pickle} module doesn't handle code objects, which the |
| 60 | \code{marshal} module does. I suppose \code{pickle} could, and maybe |
| 61 | it should, but there's probably no great need for it right now (as |
| 62 | long as \code{marshal} continues to be used for reading and writing |
| 63 | code objects), and at least this avoids the possibility of smuggling |
| 64 | Trojan horses into a program. |
| 65 | \stmodindex{marshal} |
| 66 | |
| 67 | For the benefit of persistency modules written using \code{pickle}, it |
| 68 | supports the notion of a reference to an object outside the pickled |
| 69 | data stream. Such objects are referenced by a name, which is an |
| 70 | arbitrary string of printable ASCII characters. The resolution of |
| 71 | such names is not defined by the \code{pickle} module --- the |
| 72 | persistent object module will have to implement a method |
| 73 | \code{persistent_load}. To write references to persistent objects, |
| 74 | the persistent module must define a method \code{persistent_id} which |
| 75 | returns either \code{None} or the persistent ID of the object. |
| 76 | |
| 77 | There are some restrictions on the pickling of class instances. |
| 78 | |
| 79 | First of all, the class must be defined at the top level in a module. |
| 80 | |
| 81 | Next, it must normally be possible to create class instances by |
| 82 | calling the class without arguments. If this is undesirable, the |
| 83 | class can define a method \code{__getinitargs__()}, which should |
| 84 | return a {\em tuple} containing the arguments to be passed to the |
| 85 | class constructor (\code{__init__()}). |
| 86 | \ttindex{__getinitargs__} |
| 87 | \ttindex{__init__} |
| 88 | |
| 89 | Classes can further influence how they are pickled --- if the class |
| 90 | defines the method \code{__getstate__()}, it is called and the return |
| 91 | state is pickled as the contents for the instance, and if the class |
| 92 | defines the method \code{__setstate__()}, it is called with the |
| 93 | unpickled state. (Note that these methods can also be used to |
| 94 | implement copying class instances.) If there is no |
| 95 | \code{__getstate__()} method, the instance's \code{__dict__} is |
| 96 | pickled. If there is no \code{__setstate__()} method, the pickled |
| 97 | object must be a dictionary and its items are assigned to the new |
| 98 | instance's dictionary. (If a class defines both \code{__getstate__()} |
| 99 | and \code{__setstate__()}, the state object needn't be a dictionary |
| 100 | --- these methods can do what they want.) This protocol is also used |
| 101 | by the shallow and deep copying operations defined in the \code{copy} |
| 102 | module. |
| 103 | \ttindex{__getstate__} |
| 104 | \ttindex{__setstate__} |
| 105 | \ttindex{__dict__} |
| 106 | |
| 107 | Note that when class instances are pickled, their class's code and |
| 108 | data is not pickled along with them. Only the instance data is |
| 109 | pickled. This is done on purpose, so you can fix bugs in a class or |
| 110 | add methods and still load objects that were created with an earlier |
| 111 | version of the class. If you plan to have long-lived objects that |
| 112 | will see many versions of a class, it may be worth to put a version |
| 113 | number in the objects so that suitable conversions can be made by the |
| 114 | class's \code{__setstate__()} method. |
| 115 | |
| 116 | The interface can be summarized as follows. |
| 117 | |
| 118 | To pickle an object \code{x} onto a file \code{f}, open for writing: |
| 119 | |
| 120 | \begin{verbatim} |
| 121 | p = pickle.Pickler(f) |
| 122 | p.dump(x) |
| 123 | \end{verbatim} |
| 124 | |
| 125 | To unpickle an object \code{x} from a file \code{f}, open for reading: |
| 126 | |
| 127 | \begin{verbatim} |
| 128 | u = pickle.Unpickler(f) |
| 129 | x = u.load(x) |
| 130 | \end{verbatim} |
| 131 | |
| 132 | The \code{Pickler} class only calls the method \code{f.write} with a |
| 133 | string argument. The \code{Unpickler} calls the methods \code{f.read} |
| 134 | (with an integer argument) and \code{f.readline} (without argument), |
| 135 | both returning a string. It is explicitly allowed to pass non-file |
| 136 | objects here, as long as they have the right methods. |
| 137 | |
| 138 | The following types can be pickled: |
| 139 | \begin{itemize} |
| 140 | |
| 141 | \item \code{None} |
| 142 | |
| 143 | \item integers, long integers, floating point numbers |
| 144 | |
| 145 | \item strings |
| 146 | |
| 147 | \item tuples, lists and dictionaries containing only picklable objects |
| 148 | |
| 149 | \item class instances whose \code{__dict__} or \code{__setstate__()} |
| 150 | is picklable |
| 151 | |
| 152 | \end{itemize} |
| 153 | |
| 154 | Attempts to pickle unpicklable objects will raise an exception; when |
| 155 | this happens, an unspecified number of bytes may have been written to |
| 156 | the file argument. |
| 157 | |
| 158 | It is possible to make multiple calls to \code{Pickler.dump()} or to |
| 159 | \code{Unpickler.load()}, as long as there is a one-to-one |
| 160 | correspondence between pickler and \code{Unpickler} objects and |
| 161 | between \code{dump} and \code{load} calls for any pair of |
| 162 | corresponding \code{Pickler} and \code{Unpicklers}. {\em Warning}: |
| 163 | this is intended for pickling multiple objects without intervening |
| 164 | modifications to the objects or their parts. If you modify an object |
| 165 | and then pickle it again using the same \code{Pickler} instance, the |
| 166 | object is not pickled again --- a reference to it is pickled and the |
| 167 | \code{Unpickler} will return the old value, not the modified one. (There |
| 168 | are two problems here: (a) detecting changes, and (b) marshalling a |
| 169 | minimal set of changes. I have no answers. Garbage Collection may |
| 170 | also become a problem here.) |