Best way to pickle functions

Aaron Scott aaron.hildebrandt at
Fri Apr 3 18:04:42 CEST 2009

I have a number of functions that I need to pickle without necessarily
knowing their names in advance. My first thought was to put all the
functions in a class, then pickle the class, but it doesn't really
work like I expected it to.

	import cPickle
	class PickleClass:
		def Awesome(self):
	stored = cPickle.dumps(PickleClass)
	del PickleClass
	restored = cPickle.loads(stored)


	Traceback (most recent call last):
	  File "", line 7, in <module>
		restored = cPickle.loads(stored)
	AttributeError: 'module' object has no attribute 'PickleClass'

So, the class itself isn't being pickled, just an instance of it.

This being the case, what's the best way to store these functions?
Maybe dump the class to a string and pull them back with an exec
statement when I need them?

More information about the Python-list mailing list