Python:使用“copyreg”为已有减速器的类型定义减速器
(请记住,我正在使用 Python 3 工作,因此解决方案需要在 Python 3 中工作。)
我想使用 copyreg
模块来教 Python 如何 pickle 函数。当我尝试这样做时,_Pickler
对象仍会尝试使用 save_global
函数来 pickle 函数。 (这不适用于未绑定的方法,这就是这样做的动机。)
似乎 _Pickler
首先尝试在自己的 dispatch
中查找在查看 copyreg.dispatch_table
之前要腌制的对象。我不确定这是否是故意的。
我有什么方法可以告诉Python使用我提供的reducer来pickle函数吗?
(Keep in mind I'm working in Python 3, so a solution needs to work in Python 3.)
I would like to use the copyreg
module to teach Python how to pickle functions. When I tried to do it, the _Pickler
object would still try to pickle functions using the save_global
function. (Which doesn't work for unbound methods, and that's the motivation for doing this.)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下 hack 似乎可以在 Python 3.1 中工作...:
其中,两行 hack 是:
您需要删除函数类型的
dispatch
条目,因为否则它会抢占 copyreg 注册;我认为您无法在 C 编码的 Pickler 上执行此操作,因此您需要将其设置为 Python 编码的 Pickler。使用您自己的类对
_Pickler
进行子类化,从而生成自己的dispatch
(复制父级并删除函数类型的条目),这样就不会那么麻烦了。 ,然后专门使用您的子类(及其转储方法)而不是pickle.dump
;然而,与泡菜本身的猴子修补相比,它也不太方便。The following hack seems to work in Python 3.1...:
Out of this, the two hackish lines are:
You need to remove the
dispatch
entry for functions' type because otherwise it preempts the copyreg registration; and I don't think you can do that on the C-coded Pickler so you need to set it to the Python-coded one.It would be a bit less of a hack to subclass
_Pickler
with a class of your own which makes its owndispatch
(copying the parent's and removing the entry for the function type), and then use your subclass specifically (and its dump method) rather thanpickle.dump
; however it would also be a bit less convenient that this monkeypatching of pickle itself.