Python Object Persistence: pickle, shelve, and marshal
Wednesday August 29, 2007
There are three modules for object persistence within Python:
The
The
The
pickle, shelve, and marshal. Given Python's golden rule that there should be one -- and only one -- way to do anything, the situation begs the question of why there are three modules for persistence. As with most modules and functions, what you use depends on what you need. In fact, each module has a very precise design for very specific purposes.
The
pickle module saves objects as streams of bytes. Each data file holds exactly one stream. This simplicity saves you from the overhead of a dictionary object when one is not needed.
The
shelve module, on the other hand, saves its data as a dictionary database. This saves you from a multiplicity of object files and the administration inherent in their creation and maintenance.
The
marshal module, however, is not meant to be employed for external object persistence. It is, rather, intended solely for internal use. To learn more about its precise purpose, see the Python documentation.
