Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{pickle} --- |
| 2 | Python object serialization.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{pickle} |
| 4 | |
| 5 | \modulesynopsis{Convert Python objects to streams of bytes and back.} |
| 6 | |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 7 | \index{persistency} |
| 8 | \indexii{persistent}{objects} |
| 9 | \indexii{serializing}{objects} |
| 10 | \indexii{marshalling}{objects} |
| 11 | \indexii{flattening}{objects} |
| 12 | \indexii{pickling}{objects} |
| 13 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 14 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 15 | The \module{pickle} module implements a basic but powerful algorithm for |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 16 | ``pickling'' (a.k.a.\ serializing, marshalling or flattening) nearly |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 17 | arbitrary Python objects. This is the act of converting objects to a |
| 18 | stream of bytes (and back: ``unpickling''). |
| 19 | This is a more primitive notion than |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 20 | persistency --- although \module{pickle} reads and writes file objects, |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 21 | it does not handle the issue of naming persistent objects, nor the |
| 22 | (even more complicated) area of concurrent access to persistent |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 23 | objects. The \module{pickle} module can transform a complex object into |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 24 | a byte stream and it can transform the byte stream into an object with |
| 25 | the same internal structure. The most obvious thing to do with these |
| 26 | byte streams is to write them onto a file, but it is also conceivable |
| 27 | to send them across a network or store them in a database. The module |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 28 | \module{shelve} provides a simple interface to pickle and unpickle |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 29 | objects on ``dbm''-style database files. |
Fred Drake | 54820dc | 1997-12-15 21:56:05 +0000 | [diff] [blame] | 30 | \refstmodindex{shelve} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 31 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 32 | \strong{Note:} The \module{pickle} module is rather slow. A |
| 33 | reimplementation of the same algorithm in \C{}, which is up to 1000 times |
| 34 | faster, is available as the \module{cPickle}\refbimodindex{cPickle} |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame] | 35 | module. This has the same interface except that \code{Pickler} and |
| 36 | \code{Unpickler} are factory functions, not classes (so they cannot be |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 37 | used as base classes for inheritance). |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 38 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 39 | Unlike the built-in module \module{marshal}, \module{pickle} handles |
| 40 | the following correctly: |
Fred Drake | 54820dc | 1997-12-15 21:56:05 +0000 | [diff] [blame] | 41 | \refbimodindex{marshal} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 42 | |
| 43 | \begin{itemize} |
| 44 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 45 | \item recursive objects (objects containing references to themselves) |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 47 | \item object sharing (references to the same object in different places) |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 49 | \item user-defined classes and their instances |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 50 | |
| 51 | \end{itemize} |
| 52 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 53 | The data format used by \module{pickle} is Python-specific. This has |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 54 | the advantage that there are no restrictions imposed by external |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame] | 55 | standards such as XDR% |
| 56 | \index{XDR} |
| 57 | \index{External Data Representation} |
| 58 | (which can't represent pointer sharing); however |
| 59 | it means that non-Python programs may not be able to reconstruct |
| 60 | pickled Python objects. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 61 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 62 | By default, the \module{pickle} data format uses a printable \ASCII{} |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 63 | representation. This is slightly more voluminous than a binary |
| 64 | representation. The big advantage of using printable \ASCII{} (and of |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 65 | some other characteristics of \module{pickle}'s representation) is that |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 66 | for debugging or recovery purposes it is possible for a human to read |
| 67 | the pickled file with a standard text editor. |
| 68 | |
| 69 | A binary format, which is slightly more efficient, can be chosen by |
| 70 | specifying a nonzero (true) value for the \var{bin} argument to the |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 71 | \class{Pickler} constructor or the \function{dump()} and \function{dumps()} |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 72 | functions. The binary format is not the default because of backwards |
| 73 | compatibility with the Python 1.4 pickle module. In a future version, |
| 74 | the default may change to binary. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 75 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 76 | The \module{pickle} module doesn't handle code objects, which the |
| 77 | \module{marshal} module does. I suppose \module{pickle} could, and maybe |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 78 | it should, but there's probably no great need for it right now (as |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 79 | long as \module{marshal} continues to be used for reading and writing |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 80 | code objects), and at least this avoids the possibility of smuggling |
| 81 | Trojan horses into a program. |
Fred Drake | 54820dc | 1997-12-15 21:56:05 +0000 | [diff] [blame] | 82 | \refbimodindex{marshal} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 83 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 84 | For the benefit of persistency modules written using \module{pickle}, it |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 85 | supports the notion of a reference to an object outside the pickled |
| 86 | data stream. Such objects are referenced by a name, which is an |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 87 | arbitrary string of printable \ASCII{} characters. The resolution of |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 88 | such names is not defined by the \module{pickle} module --- the |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 89 | persistent object module will have to implement a method |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 90 | \method{persistent_load()}. To write references to persistent objects, |
| 91 | the persistent module must define a method \method{persistent_id()} which |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 92 | returns either \code{None} or the persistent ID of the object. |
| 93 | |
| 94 | There are some restrictions on the pickling of class instances. |
| 95 | |
| 96 | First of all, the class must be defined at the top level in a module. |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 97 | Furthermore, all its instance variables must be picklable. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 98 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 99 | \setindexsubitem{(pickle protocol)} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 100 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 101 | When a pickled class instance is unpickled, its \method{__init__()} method |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 102 | is normally \emph{not} invoked. \strong{Note:} This is a deviation |
| 103 | from previous versions of this module; the change was introduced in |
| 104 | Python 1.5b2. The reason for the change is that in many cases it is |
| 105 | desirable to have a constructor that requires arguments; it is a |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 106 | (minor) nuisance to have to provide a \method{__getinitargs__()} method. |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 107 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 108 | If it is desirable that the \method{__init__()} method be called on |
| 109 | unpickling, a class can define a method \method{__getinitargs__()}, |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame] | 110 | which should return a \emph{tuple} containing the arguments to be |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 111 | passed to the class constructor (\method{__init__()}). This method is |
Guido van Rossum | 5793039 | 1997-12-30 17:44:48 +0000 | [diff] [blame] | 112 | called at pickle time; the tuple it returns is incorporated in the |
| 113 | pickle for the instance. |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 114 | \ttindex{__getinitargs__()} |
| 115 | \ttindex{__init__()} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 117 | Classes can further influence how their instances are pickled --- if the class |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 118 | defines the method \method{__getstate__()}, it is called and the return |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 119 | state is pickled as the contents for the instance, and if the class |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 120 | defines the method \method{__setstate__()}, it is called with the |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 121 | unpickled state. (Note that these methods can also be used to |
| 122 | implement copying class instances.) If there is no |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 123 | \method{__getstate__()} method, the instance's \member{__dict__} is |
| 124 | pickled. If there is no \method{__setstate__()} method, the pickled |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 125 | object must be a dictionary and its items are assigned to the new |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 126 | instance's dictionary. (If a class defines both \method{__getstate__()} |
| 127 | and \method{__setstate__()}, the state object needn't be a dictionary |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 128 | --- these methods can do what they want.) This protocol is also used |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 129 | by the shallow and deep copying operations defined in the \module{copy} |
| 130 | module.\refstmodindex{copy} |
| 131 | \ttindex{__getstate__()} |
| 132 | \ttindex{__setstate__()} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 133 | \ttindex{__dict__} |
| 134 | |
| 135 | Note that when class instances are pickled, their class's code and |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 136 | data are not pickled along with them. Only the instance data are |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 137 | pickled. This is done on purpose, so you can fix bugs in a class or |
| 138 | add methods and still load objects that were created with an earlier |
| 139 | version of the class. If you plan to have long-lived objects that |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 140 | will see many versions of a class, it may be worthwhile to put a version |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 141 | number in the objects so that suitable conversions can be made by the |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 142 | class's \method{__setstate__()} method. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 144 | When a class itself is pickled, only its name is pickled --- the class |
| 145 | definition is not pickled, but re-imported by the unpickling process. |
| 146 | Therefore, the restriction that the class must be defined at the top |
| 147 | level in a module applies to pickled classes as well. |
| 148 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 149 | \setindexsubitem{(in module pickle)} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 151 | The interface can be summarized as follows. |
| 152 | |
| 153 | To pickle an object \code{x} onto a file \code{f}, open for writing: |
| 154 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 155 | \begin{verbatim} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 156 | p = pickle.Pickler(f) |
| 157 | p.dump(x) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 158 | \end{verbatim} |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 160 | A shorthand for this is: |
| 161 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 162 | \begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 163 | pickle.dump(x, f) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 164 | \end{verbatim} |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 166 | To unpickle an object \code{x} from a file \code{f}, open for reading: |
| 167 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 168 | \begin{verbatim} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 169 | u = pickle.Unpickler(f) |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 170 | x = u.load() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 171 | \end{verbatim} |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 173 | A shorthand is: |
| 174 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 175 | \begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 176 | x = pickle.load(f) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 177 | \end{verbatim} |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 178 | |
| 179 | The \class{Pickler} class only calls the method \code{f.write()} with a |
| 180 | string argument. The \class{Unpickler} calls the methods \code{f.read()} |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame] | 181 | (with an integer argument) and \code{f.readline()} (without argument), |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 182 | both returning a string. It is explicitly allowed to pass non-file |
| 183 | objects here, as long as they have the right methods. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 184 | \ttindex{Unpickler} |
| 185 | \ttindex{Pickler} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 186 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 187 | The constructor for the \class{Pickler} class has an optional second |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 188 | argument, \var{bin}. If this is present and nonzero, the binary |
| 189 | pickle format is used; if it is zero or absent, the (less efficient, |
| 190 | but backwards compatible) text pickle format is used. The |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 191 | \class{Unpickler} class does not have an argument to distinguish |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 192 | between binary and text pickle formats; it accepts either format. |
| 193 | |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 194 | The following types can be pickled: |
| 195 | \begin{itemize} |
| 196 | |
| 197 | \item \code{None} |
| 198 | |
| 199 | \item integers, long integers, floating point numbers |
| 200 | |
| 201 | \item strings |
| 202 | |
| 203 | \item tuples, lists and dictionaries containing only picklable objects |
| 204 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 205 | \item classes that are defined at the top level in a module |
| 206 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 207 | \item instances of such classes whose \member{__dict__} or |
| 208 | \method{__setstate__()} is picklable |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 209 | |
| 210 | \end{itemize} |
| 211 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 212 | Attempts to pickle unpicklable objects will raise the |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 213 | \exception{PicklingError} exception; when this happens, an unspecified |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 214 | number of bytes may have been written to the file. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 215 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 216 | It is possible to make multiple calls to the \method{dump()} method of |
| 217 | the same \class{Pickler} instance. These must then be matched to the |
| 218 | same number of calls to the \method{load()} method of the |
| 219 | corresponding \class{Unpickler} instance. If the same object is |
| 220 | pickled by multiple \method{dump()} calls, the \method{load()} will all |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame] | 221 | yield references to the same object. \emph{Warning}: this is intended |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 222 | for pickling multiple objects without intervening modifications to the |
| 223 | objects or their parts. If you modify an object and then pickle it |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 224 | again using the same \class{Pickler} instance, the object is not |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 225 | pickled again --- a reference to it is pickled and the |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 226 | \class{Unpickler} will return the old value, not the modified one. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 227 | (There are two problems here: (a) detecting changes, and (b) |
| 228 | marshalling a minimal set of changes. I have no answers. Garbage |
| 229 | Collection may also become a problem here.) |
| 230 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 231 | Apart from the \class{Pickler} and \class{Unpickler} classes, the |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 232 | module defines the following functions, and an exception: |
| 233 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 234 | \begin{funcdesc}{dump}{object, file\optional{, bin}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 235 | Write a pickled representation of \var{obect} to the open file object |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 236 | \var{file}. This is equivalent to |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 237 | \samp{Pickler(\var{file}, \var{bin}).dump(\var{object})}. |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 238 | If the optional \var{bin} argument is present and nonzero, the binary |
| 239 | pickle format is used; if it is zero or absent, the (less efficient) |
| 240 | text pickle format is used. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 241 | \end{funcdesc} |
| 242 | |
| 243 | \begin{funcdesc}{load}{file} |
| 244 | Read a pickled object from the open file object \var{file}. This is |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 245 | equivalent to \samp{Unpickler(\var{file}).load()}. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 246 | \end{funcdesc} |
| 247 | |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 248 | \begin{funcdesc}{dumps}{object\optional{, bin}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 249 | Return the pickled representation of the object as a string, instead |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 250 | of writing it to a file. If the optional \var{bin} argument is |
| 251 | present and nonzero, the binary pickle format is used; if it is zero |
| 252 | or absent, the (less efficient) text pickle format is used. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 253 | \end{funcdesc} |
| 254 | |
| 255 | \begin{funcdesc}{loads}{string} |
| 256 | Read a pickled object from a string instead of a file. Characters in |
| 257 | the string past the pickled object's representation are ignored. |
| 258 | \end{funcdesc} |
| 259 | |
| 260 | \begin{excdesc}{PicklingError} |
| 261 | This exception is raised when an unpicklable object is passed to |
| 262 | \code{Pickler.dump()}. |
| 263 | \end{excdesc} |
Fred Drake | 4074896 | 1998-03-06 21:27:14 +0000 | [diff] [blame] | 264 | |
| 265 | |
| 266 | \begin{seealso} |
| 267 | \seemodule[copyreg]{copy_reg}{pickle interface constructor |
| 268 | registration} |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 269 | |
Fred Drake | 9b28fe2 | 1998-04-04 06:20:28 +0000 | [diff] [blame] | 270 | \seemodule{shelve}{indexed databases of objects; uses \module{pickle}} |
Fred Drake | 17e5640 | 1998-04-11 20:43:51 +0000 | [diff] [blame] | 271 | |
| 272 | \seemodule{copy}{shallow and deep object copying} |
| 273 | |
| 274 | \seemodule{marshal}{high-performance serialization of built-in types} |
Fred Drake | 4074896 | 1998-03-06 21:27:14 +0000 | [diff] [blame] | 275 | \end{seealso} |
Fred Drake | 9463de2 | 1998-04-11 20:05:43 +0000 | [diff] [blame] | 276 | |
| 277 | |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 278 | \section{\module{cPickle} --- |
| 279 | Alternate implementation of \module{pickle}.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 280 | \declaremodule{builtin}{cPickle} |
| 281 | |
| 282 | \modulesynopsis{Faster version of \module{pickle}, but not subclassable.} |
| 283 | |
Fred Drake | 9463de2 | 1998-04-11 20:05:43 +0000 | [diff] [blame] | 284 | |
| 285 | % This section was written by Fred L. Drake, Jr. <fdrake@acm.org> |
| 286 | |
| 287 | The \module{cPickle} module provides a similar interface and identical |
| 288 | functionality as the \module{pickle} module, but can be up to 1000 |
| 289 | times faster since it is implemented in \C{}. The only other |
| 290 | important difference to note is that \function{Pickler()} and |
| 291 | \function{Unpickler()} are functions and not classes, and so cannot be |
| 292 | subclassed. This should not be an issue in most cases. |
| 293 | |
| 294 | The format of the pickle data is identical to that produced using the |
| 295 | \module{pickle} module, so it is possible to use \module{pickle} and |
| 296 | \module{cPickle} interchangably with existing pickles. |
Guido van Rossum | cf3ce92 | 1999-01-06 23:34:39 +0000 | [diff] [blame] | 297 | |
| 298 | (Since the pickle data format is actually a tiny stack-oriented |
| 299 | programming language, and there are some freedoms in the encodings of |
| 300 | certain objects, it's possible that the two modules produce different |
| 301 | pickled data for the same input objects; however they will always be |
| 302 | able to read each others pickles back in.) |