On Mon, Jul 13, 2009 at 2:14 PM, Aaron Scott<aaron.hildebra...@gmail.com> wrote: > I'm trying to pickle an instance of a class. It mostly works just fine > -- I can save the pickle to a file, restore it from that file, and > it's mostly okay. The problem is, some lists seem to disappear. For > example (snipped and crunched from the code giving me trouble): > > --- > > > class InitGame: > value = False > journal = [] > > > game.InitGame()
That line doesn't make sense with the code you've given... > def Save(): > global game > import cPickle, gzip, os > > # Change some data > game.journal.append("A value") > game.value = True > > pickles = {'game': game} > jar = gzip.open("pickefile", 'wb') > cPickle.dump(pickles, jar, 2) > jar.close() > > > def Load(): > global game > import gzip, os, cPickle > jar = gzip.open("picklefile", 'r') > loaded = cPickle.load(jar) > jar.close() > game = loaded["game"] > > > --- > > Now, if I save a pickle, then load it back in, I'll get an instance of > InitGame called "game", and game.value will be true, but the list > "journal" will be empty. > > Am I missing something obvious about the pickle spec? Am I doing > something wrong? Or should I be hunting for a deeper bug in the code? Your class definition isn't right. It makes 'value' and 'journal' class variables (Java lingo: "static variables") as opposed to the instance variables they should be. Here's a corrected version: class InitGame(object): def __init__(self): #instance variables are created through self.foo assignments in __init__ self.value = False self.journal = [] Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list