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.