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

Pickling Objects inherited from dict (Bug?)

P: n/a
Hi!

After unpickling the objects are not the same
any more. Is this a bug or feature?

import pickle

class MyDictContainer(dict):
def __init__(self):
dict.__init__(self)
print "MyDictContainer.__init__"

class MyDict(dict):
def __init__(self, root, name):
dict.__init__(self)
self.root=root
self.name=name

container=MyDictContainer()
for name in ["one", "two", "three"]:
mydict=MyDict(container, name)
container[name]=mydict

print container
file="data.pickle"
fd=open(file, "w")
pickle.dump(container, fd)
fd.close()

fd=open(file)
unpickle=pickle.load(fd)
fd.close()
print unpickle
Output:

===> python test-pickle.py
MyDictContainer.__init__
{'three': {}, 'two': {}, 'one': {}}
{'one': {},
'three': (<class '__main__.MyDictContainer'>,
<type 'dict'>, {'three': {},
'two': (<class '__main__.MyDictContainer'>, <type 'dict'>,
{'three': {}, 'two': {}, 'one': {}}), 'one': {}}), 'two':
(<class '__main__.MyDictContainer'>, <type 'dict'>,
{'three': {}, 'two': {}, 'one': {}})}

After unpickling the object is not the same anymore.
Any hints?

If I inherit from UserDict instead, the is no error.

thomas

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Thomas Guettler" <gu*****@thomas-guettler.de> writes:
Hi!

After unpickling the objects are not the same
any more. Is this a bug or feature?


There's a bug report up on SF that may be relavent, try searching
(unfortunately I don't remember the details...).

Cheers,
mwh

--
The use of COBOL cripples the mind; its teaching should, therefore,
be regarded as a criminal offence.
-- Edsger W. Dijkstra, SIGPLAN Notices, Volume 17, Number 5
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.