使用reducer函数python感受,Python:使用`copyreg`为已经有reducer的类型定义reducer

(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?

解决方案

The following hack seems to work in 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)

Out of this, the two hackish lines are:

pickle.Pickler = pickle._Pickler

del pickle.Pickler.dispatch[ft]

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 own dispatch (copying the parent's and removing the entry for the function type), and then use your subclass specifically (and its dump method) rather than pickle.dump; however it would also be a bit less convenient that this monkeypatching of pickle itself.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值