Ram*_*hum 5 python function pickle
(请记住我在Python 3中工作,因此解决方案需要在Python 3中工作.)
我想用这个copyreg模块教Python如何pickle函数.当我尝试这样做时,该_Pickler对象仍然会尝试使用该save_global函数来pickle 函数.(这对于未绑定的方法不起作用,这就是这样做的动机.)
It seems like _Pickler first tries to look in its own dispatch for the type of the object that you want to pickle before looking in copyreg.dispatch_table. I'm not sure if this is intentional.
Is there any way for me to tell Python to pickle functions with the reducer that I provide?
以下 hack 似乎适用于 Python 3.1...:
import copyreg
def functionpickler(f):
print('pickling', f.__name__)
return f.__name__
ft = type(functionpickler)
copyreg.pickle(ft, functionpickler)
import pickle
pickle.Pickler = pickle._Pickler
del pickle.Pickler.dispatch[ft]
s = pickle.dumps(functionpickler)
print('Result is', s)
Run Code Online (Sandbox Code Playgroud)
其中,两条黑线是:
pickle.Pickler = pickle._Pickler
del pickle.Pickler.dispatch[ft]
Run Code Online (Sandbox Code Playgroud)
您需要删除dispatch函数类型的条目,否则它会抢占 copyreg 注册;我认为您无法在 C 编码的 Pickler 上执行此操作,因此您需要将其设置为 Python 编码的 Pickler。
_Pickler使用您自己的类进行子类化dispatch(复制父类并删除函数类型的条目),然后专门使用您的子类(及其转储方法)而不是pickle.dump;然而,与泡菜本身的猴子修补相比,它也不太方便。
| 归档时间: |
|
| 查看次数: |
454 次 |
| 最近记录: |