Pickling object inherited from dict
Thomas Guettler
guettli at thomas-guettler.de
Tue Oct 28 04:04:15 EST 2003
Am Mon, 27 Oct 2003 17:19:08 +0100 schrieb Thomas Guettler:
> Hi!
>
> If I pickle a class which inherits from dict,
> I don't get the same data again, if I unpickle
> it.
Here is a executable example:
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?
thomas
More information about the Python-list
mailing list