Pickling classes -- disappearing lists?

Aaron Scott aaron.hildebrandt at gmail.com
Mon Jul 13 23:14:27 CEST 2009

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 = []


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)

def Load():
	global game
	import gzip, os, cPickle
	jar = gzip.open("picklefile", 'r')
	loaded = cPickle.load(jar)
	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).

More information about the Python-list mailing list