The pickle
module is used for serializing objects.
http://docs.python.org/2/library/pickle.html
You can usually just use it as-is, but if you need to define how objects should be serialized, you can override the special methods, __getstate__
and __setstate__
import cPickle as pickle # faster implementation
path = 'test.dat'
obj = ('Hello, world!', 123, {'x': 0})
# save to disk
with open(path, 'wb') as fp:
pickle.dump(obj, fp)
# load from disk
with open(path, 'rb') as fp:
obj = pickle.load(fp)
print obj