Object Persistence Modules

Three modules comprise the object persistence interface.

dbm (anydbm in Python 2.X)

Key-based string-only storage files.

pickle (and cPickle in Python 2.X)

Serializes an in-memory object to/from file streams.

shelve

Key-based persistent object stores: pickles objects to/from dbm files.

The shelve module implements persistent object stores. shelve in turn uses the pickle module to convert (serialize) in-memory Python objects to byte-stream strings and the dbm module to store serialized byte-stream strings in access-by-key files.

Note

In Python 2.X, dbm is named anydbm, and the cPickle module is an optimized version of pickle that may be imported directly and is used automatically by shelve, if present. In Python 3.0, cPickle is renamed _pickle and is automatically used by pickle if present—it need not be imported directly and is acquired by shelve.

Also note that in Python 3.0 the Berkeley DB (a.k.a. bsddb) interface for dbm is no longer shipped with Python itself, but is a third-party open source extension which must be installed separately (see the Python 3.0 Library Reference for resources).

dbm and shelve Modules

dbm is an access-by-key filesystem: strings are stored and fetched by their string keys. The dbm module selects the keyed-access file implementation in your Python interpreter and presents a dictionary-like API for scripts. A persistent object shelve is used like a simple dbm file, except that the dbm module is replaced by shelve, and the stored value ...

Get Python Pocket Reference, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.