Pickling Python 3 中的未绑定方法
我想在 Python 3.x 中 pickle 一个未绑定的方法。我收到此错误:
>>> class A:
... def m(self):
... pass
>>> import pickle
>>> pickle.dumps(A.m)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
pickle.dumps(A.m)
File "C:\Python31\lib\pickle.py", line 1358, in dumps
Pickler(f, protocol, fix_imports=fix_imports).dump(obj)
_pickle.PicklingError: Can't pickle <class 'function'>: attribute lookup builtins.function failed
有人有这方面的经验吗?
注意:在 Python 2.x 中,默认情况下也不可能 pickle 未绑定的方法;我设法以某种我不明白的奇怪方式做到了这一点:我用 MethodType 类的 copy_reg
模块编写了一个减速器,它涵盖了绑定和非绑定方法。但reducer只解决了绑定方法的情况,因为它依赖于my_method.im_self
。神秘的是,它还使得 Python 2.x 能够 pickle 未绑定的方法。这在 Python 3.x 上不会发生。
I would like to pickle an unbound method in Python 3.x. I'm getting this error:
>>> class A:
... def m(self):
... pass
>>> import pickle
>>> pickle.dumps(A.m)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
pickle.dumps(A.m)
File "C:\Python31\lib\pickle.py", line 1358, in dumps
Pickler(f, protocol, fix_imports=fix_imports).dump(obj)
_pickle.PicklingError: Can't pickle <class 'function'>: attribute lookup builtins.function failed
Does anyone have experience with this?
Note: In Python 2.x it's also impossible to pickle unbound methods by default; I managed to do it there in some weird way I don't understand: I wrote a reducer with the copy_reg
module for the MethodType class, which covers both bound and unbound methods. But the reducer only solved the case of the bound method, because it depended on my_method.im_self
. Mysteriously it has also caused Python 2.x to be able to pickle unbound methods. This does not happen on Python 3.x.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不能直接完成,因为在 Python 3 中,未绑定方法类型消失了:它只是一个函数:
Python 函数没有绑定到类,因此不可能仅通过以下方式来判断
Am
属于哪个类查看表达式结果。根据您的具体需要,腌制/取消腌制(类,方法名称)的元组可能就足够了:
您可以简单地使用
getattr()
从这里获取方法(函数):This cannot be done directly because in Python 3 unbound method type is gone: it is just a function:
Python functions are not bound to a class, so it is impossible to tell what class
A.m
belongs to just by looking at the expression result.Depending on what exactly you need, pickling/unpickling a tuple of (class, method-name) might be good enough:
You can get the method (function) from here simply by using
getattr()
: