Pickling object inherited from dict

From: Thomas Guettler (guettli_at_thomas-guettler.de)
Date: 10/27/03


Date: Mon, 27 Oct 2003 17:19:08 +0100

Hi!

If I pickle a class which inherits from dict,
I don't get the same data again, if I unpickle
it.

def test_pickle():
    home="..."
    server=WorkflowServer(home)
    server.save()
    print server.users.items() # 1
    unpickle=load(home)
    print unpickle.users.items() #2
    
def load(home):
    file=os.path.join(home, "data.pickle")
    fd=open(file)
    server=pickle.load(fd)
    fd.close()
    assert(server.__class__==WorkflowServer)
    return server

WorkflowServer:
def save(self):
        file=os.path.join(self.home, "data.pickle")
        fd=open(file, "w")
        pickle.dump(self, fd)
        fd.close()

class User(dict):
     ....

class UserManagemente(dict):
     .... # server.users

If I run test_pickle() I get this result: (See #1 and #2)
[('EGSADMIN', {}), ('TEST', {})]
[('EGSADMIN', {}), ('TEST', (<class '__main__.UserManagement'>,
     <type 'dict'>, {'EGSADMIN': {}, 'TEST': {}}))]

Why are the two lines different? Sorry, I was not able
to make a smaller working example today.

 thomas

Python 2.2.2



Relevant Pages

  • Emulating Python Inheritance Manually
    ... def custom_print(self, *args): ... if not inherits: ... # NAME Horse ...
    (comp.lang.python)
  • Re: Missing member
    ... inherits from A and C inherits from B (hope I used the right words ... def move: ... # """ changes the angle so the entity "looks" at the specified coords ... """ blits the entire stats.image onto the targetSurface at the ent's coords """ ...
    (comp.lang.python)
  • Re: How to replace a method in an instance.
    ... One of the classes inherits a method that he shouldn't. ... but since in the real world, CC inherits from his baseclass, the above assignment causes the baseclass to be altered. ... def f1: ... for fname, f in replacements.items: ...
    (comp.lang.python)
  • Re: overriding method that returns base class object
    ... I mean B inherits from ... def a: ... This is all in Python 2.3.3. ... >> I have a class A from a third party that I cannot change ...
    (comp.lang.python)
  • Re: Will python never intend to support private, protected and public?
    ... > def gethidden: ... accident (A inherits from B, and then something imports B and makes an ... class Secret(Parrot): # I'm not aware that you also have a Secret class ...
    (comp.lang.python)