Difference between Pickle and Shelve?

K

Kris Caselden

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?
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top