我试图通过提供自定义序列化功能来解决使用原生pickle模块的lambdas:
from six.moves import copyreg
import pickle, types
copyreg.pickle(types.FunctionType, _custom_pickle_func)
但是,这不适用于以下类型的pickle支持本地:
import pickle
pickle._Pickler.dispatch
{NoneType: <function pickle._Pickler.save_none>,
bool: <function pickle._Pickler.save_bool>,
bytes: <function pickle._Pickler.save_bytes>,
dict: <function pickle._Pickler.save_dict>,
float: <function pickle._Pickler.save_float>,
frozenset: <function pickle._Pickler.save_frozenset>,
function: <function pickle._Pickler.save_global>,
int: <function pickle._Pickler.save_long>,
list: <function pickle._Pickler.save_list>,
set: <function pickle._Pickler.save_set>,
str: <function pickle._Pickler.save_str>,
tuple: <function pickle._Pickler.save_tuple>,
type: <function pickle._Pickler.save_type>}
这是因为C {pickle._Pickler}首先在自己的调度表C {pickle.dispatch}中查找条目,然后在C {copyreg.dispath_table}中查找条目。这可以防止我们的注册处理程序发挥作用。这里的解决方法是在_pickle和pickle中覆盖本机类型的调度函数。 我们可以像dill那样为纯Python C {pickle._Pickler}执行此操作,但是为_pickle覆盖这些条目似乎是不可能的,因为_pickle.Pickler.dispatch_table的类型是 member_descriptor ,它不支持赋值来自Python解释器。如果我错了或者有任何方法可以覆盖_pickle,请纠正我。