Message345721
if we create a bound method object `f` with function object `A.f` and instance object `B()`,
when pickling this bound method object:
import pickle
class A():
def f(self):
pass
class B():
def f(self):
pass
o = B()
f = A.f.__get__(o)
pf = pickle.loads(pickle.dumps(f))
print(f)
print(pf)
we get:
<bound method A.f of <__main__.B object at 0x10b82ac18>>
<bound method B.f of <__main__.B object at 0x10b82ac88>>
the underlaying function are lost, changed from `A.f` to `B.f`.
as pickle calls `__reduce__` method of `method object`, IMO [its implementation][1] simply ignored the real function, whcih is not right.
I have tried a [wordaround][2]:
import types
import copyreg
def my_reduce(obj):
return (obj.__func__.__get__, (obj.__self__,))
copyreg.pickle(types.MethodType, my_reduce)
[1]: https://github.com/python/cpython/blob/v3.7.3/Objects/classobject.c#L75-L89
[2]: https://stackoverflow.com/a/56614748/4201810 |
|
Date |
User |
Action |
Args |
2019-06-16 01:40:57 | georgexsh | set | recipients:
+ georgexsh |
2019-06-16 01:40:57 | georgexsh | set | messageid: <1560649257.15.0.526142047149.issue37297@roundup.psfhosted.org> |
2019-06-16 01:40:57 | georgexsh | link | issue37297 messages |
2019-06-16 01:40:56 | georgexsh | create | |
|