Probably the biggest limitation of DBM keyed files is in what they
can store: data stored under a key must be a simple 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 takes you only so far.
For arbitrarily complex Python objects such as class instances and
nested data structures, you need something more. Class instance
objects, for example, cannot usually be later re-created from their
standard string representations. Moreover, custom to-string
conversions and from-string parsers are error prone and not
pickle module, a
standard part of the Python system, provides the
conversion step needed. It’s a sort of super general data formatting
and de-formatting tool—
pickle converts nearly arbitrary Python
in-memory objects to and from a single linear string format, suitable
for storing in flat files, shipping across network sockets between
trusted sources, 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 format. It retains all the content and references structure of the original in-memory object. When the object is later re-created from its byte string, it will be a new in-memory object identical in structure and value ...