Pickling Objects inherited from dict (Bug?)
Thomas Guettler
guettli at thomas-guettler.de
Wed Nov 5 02:46:47 EST 2003
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
More information about the Python-list
mailing list