Pickling Objects inherited from dict (Bug?)

Discussion in 'Python' started by Thomas Guettler, Nov 5, 2003.

  1. 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
    Thomas Guettler, Nov 5, 2003
    #1
    1. Advertising

  2. "Thomas Guettler" <> 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
    Michael Hudson, Nov 5, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Guettler

    Pickling object inherited from dict

    Thomas Guettler, Oct 27, 2003, in forum: Python
    Replies:
    1
    Views:
    293
    Thomas Guettler
    Oct 28, 2003
  2. Edward Loper

    pickling subclasses of dict/list

    Edward Loper, Jul 3, 2004, in forum: Python
    Replies:
    1
    Views:
    402
    Greg Chapman
    Jul 4, 2004
  3. Dan Perl
    Replies:
    0
    Views:
    354
    Dan Perl
    Nov 3, 2004
  4. Alex
    Replies:
    9
    Views:
    370
  5. Klaus Kopec
    Replies:
    4
    Views:
    455
    Klaus Kopec
    Dec 10, 2008
Loading...

Share This Page