Pickling classes -- disappearing lists?

A

Aaron Scott

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()


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?
I've noticed that pretty much anything that's a list isn't saving to
the pickle (or loading from the pickle... it's hard to tell which).
 
C

Chris Rebert

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
 
A

Aaron Scott

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:

Woah, thanks. I can't believe I made such a stupid mistake. It's not
like I'm a newcomer to Python, either. I'm can't believe I never
noticed what I was doing.

No more 2am coding for me.

Thanks,
Aaron
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top