I want to do the following:
import pickle
namespace = {}
exec('def f(x): return x', namespace)
pickle.dumps(namespace['f'])
However, this results in the following error:
---------------------------------------------------------------------------
PicklingError Traceback (most recent call last)
<ipython-input-102-61493bb3c732> in <module>()
2 namespace = {}
3 exec('def f(x): return x', namespace)
----> 4 pickle.dumps(namespace['f'])
PicklingError: Can't pickle <function f at 0x7f2134171950>: it's not the same object as __main__.f
The problem that I want to solve: I have a function in string format and I need to be able to serialize it (for parallelization purposes.)