Probably the biggest limitation of DBM
keyed files is in what they can store: data stored under a key must
be a simple text string. If you want to store Python objects in a DBM
file, you can sometimes manually convert them to and from strings on
writes and reads (e.g., with
eval calls), but this only takes you so far. For
arbitrarily complex Python objects like class instances, you need
something more. Class instance objects, for example, cannot be later
recreated from their standard string representations.
module, a standard part of the Python system, provides the conversion
step needed. It converts Python in-memory objects to and from a
single linear string format, suitable for storing in flat files,
shipping across network sockets, and so on. This conversion from
object to string is often called serialization -- arbitrary data structures in memory are mapped to a
serial string form. The string representation used for objects is
also sometimes referred to as a byte-stream, due to its linear
Pickling may sound complicated the first time you encounter it, but the good news is that Python hides all the complexity of object-to-string conversion. In fact, the pickle module’s interfaces are incredibly simple to use. The following list describes a few details of this interface.
P = pickle.Pickler(
Make a new pickler for pickling to an open output file object
Write an object onto ...