Difference between Pickle and Shelve?

Martin v. Löwis martin at v.loewis.de
Sun Oct 12 14:14:50 EDT 2003


google at hanger.snowbird.net (Kris Caselden) writes:

> Python's docs say that Shelve uses Pickle to serialize its data.
> However, I've noticed that Pickle can maintain internal links, while
> Shelve cannot.

shelve also maintains internal links - just not links across different
keys.

> Pickle is a bit more work, but it's able to maintain these internal
> links AND generates a considerable smaller file (44bytes with Pickle
> compared to 24Kbytes with Shelve). Why does Shelve create such huge
> files?

shelve is a database system capable of storing multiple objects in a
single file. Each individual object is stored with pickle.

So if you want to store only a single (root) object; if you want to
independently load and store independent objects, use shelve.

Regards,
Martin




More information about the Python-list mailing list