There are three questions as possible duplicates (but too specific):
Just use stackless python. You can serialize almost anything with pickle, including functions. Here I serialize and deserialize a lambda using the pickle module. This is similar to what you are trying to do in your example.
Here is the download link for Stackless Python http://www.stackless.com/wiki/Download
Python 2.7.5 Stackless 3.1b3 060516 (default, Sep 23 2013, 20:17:03)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = 5
>>> g = lambda : f * f
>>> g()
25
>>> import pickle
>>> p = pickle.dumps(g)
>>> m = pickle.loads(p)
>>> m()
25
>>>