我想做以下事情:
import pickle
namespace = {}
exec('def f(x): return x', namespace)
pickle.dumps(namespace['f'])
但是,这会导致以下错误:
---------------------------------------------------------------------------
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
我想解决的问题:我有一个字符串格式的函数,我需要能够序列化它(用于并行化目的。)