This is an old copy of the Python FAQ. The information here may be outdated.

How do you implement persistent objects in Python?

The pickle library module allows you to convert almost arbitrary Python objects to byte strings, and back again (it supports most data structures, include user-defined classes, though you still can’t store things like open files, sockets or windows). For better performance, you can use the cPickle module.

The shelve library module uses pickle and anydbm to create persistent mappings containing arbitrary Python objects.

An alternative is to use pickle’s little sister, marshal. This module provides very fast ways to store noncircular basic Python types to files and strings, and back again. Although marshal does not do fancy things like store instances or handle shared references properly, it does run extremely fast. For example loading a half megabyte of data can take a fraction of a second. This often beats doing something more complex and general such as using gdbm with pickle/shelve.

CATEGORY: library

 

A Django site. rendered by a django application. hosted by webfaction.