Pickling object inherited from dict

Thomas Guettler guettli at thomas-guettler.de
Mon Oct 27 11:19:08 EST 2003


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






More information about the Python-list mailing list