By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,928 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,928 IT Pros & Developers. It's quick & easy.

Difference between Pickle and Shelve?

P: n/a
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?
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
go****@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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.