Best way to pickle functions
azrael
jura.grozni at gmail.com
Sun Apr 5 08:16:19 EDT 2009
As in Python everythong is an object you could use __name__.
>>> import cPickle
>>> def def1():
... pass
>>> def def2():
... pass
>>> def1.__name__
def1
>>> def2.__name__
def2
in this case you can combine __name__ to get the object name and then
combine it with eval to pickle.
>>> pickleString = "stored = cPickle.dumps(" + def1.__name__ + ")"
>>> eval(pickleString)
>>> restored = cPickle.loads(stored)
in this way you deal with dynamic strings and you can pickle anything
More information about the Python-list
mailing list