I have read in a number of threads that Python pickle
/cPickle
cannot pickle lambda functions. However the following code works, using Python 2.7.6:
import cPickle as pickle
if __name__ == "__main__":
s = pickle.dumps(lambda x, y: x+y)
f = pickle.loads(s)
assert f(3,4) == 7
So what is going on? Or, rather, what is the limit of pickling lambdas?
[EDIT] I think i know why this code runs. I forgot (sorry!) i am running stackless python, which has a form of micro-threads called tasklets executing a function. These tasklets can be halted, pickled, unpickled and continued, so i guess (asked on the stackless mailing list) that it also provides a way to pickle function bodies.