Best way to pickle functions

Aaron Scott aaron.hildebrandt at gmail.com
Fri Apr 3 12:04:42 EDT 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):
			pass
	stored = cPickle.dumps(PickleClass)
	del PickleClass
	restored = cPickle.loads(stored)

Results:

	Traceback (most recent call last):
	  File "pickletest.py", 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