Difference between Pickle and Shelve?

Kris Caselden google at hanger.snowbird.net
Sun Oct 12 13:43:30 EDT 2003


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. For instance:

>>> d = shelve.open('shelvedata.txt',writeback=True)
>>> d['a']=[1]
>>> d['b']=d['a']
>>> print d {'a': [1], 'b': [1]}
>>> d['a'][0]=2
>>> print d {'a': [2], 'b': [2]}
>>> d.close()
>>> d = shelve.open('shelvedata.txt',writeback=True)
>>> print d {'a': [2], 'b': [2]}
>>> d['a'][0]=3
>>> print d {'a': [3], 'b': [2]} <- BAD

>>> d={}
>>> d['a']=[1]
>>> d['b']=d['a']
>>> d
{'a': [1], 'b': [1]}
>>> d['a'][0]=2
>>> d
{'a': [2], 'b': [2]}
>>> outfile=open('pickledata.txt','w')
>>> import pickle
>>> pickle.dump(d,outfile)
>>> outfile.close()
>>> infile=open('pickledata.txt','r')
>>> d=pickle.load(infile)
>>> infile.close()
>>> d
{'a': [2], 'b': [2]}
>>> d['a'][0]=3
>>> d
{'a': [3], 'b': [3]} <- GOOD

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? However, since Shelve is slightly more streamlined, is there
any way to get the same functionality out of it as Pickle? If Shelve
uses Pickle why is there any discontinuity?




More information about the Python-list mailing list