Why can pickle serialize references to functions, but not methods?
Pickling a function serializes the function name, but pickling a
staticmethod, classmethod, or instancemethod generates an error. In
these cases, pickle knows the instance or class, and the method, so
what's the problem? Pickle doesn't serialize code objects, so why can't
it serialize the name as it does for functions? Is this one of those
features that's feasible, but not useful, so no one's ever gotten
around to implementing it?
Regards,
Chris
.... return 1
....
.... @staticmethod
.... def somefunc():
.... return 1
....
.... pass
....
ccopy_reg
_reconstructor
p0
(c__main__
Foo
p1
c__builtin__
object
p2
Ntp3
Rp4
(dp5
S'value'
p6
c__main__
somefunc
p7
sb.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Program Files\Python24\ lib\pickle.py", line 1386, in dumps
Pickler(file, protocol, bin).dump(obj)
File "C:\Program Files\Python24\ lib\pickle.py", line 231, in dump
self.save(obj)
File "C:\Program Files\Python24\ lib\pickle.py", line 338, in save
self.save_reduc e(obj=obj, *rv)
File "C:\Program Files\Python24\ lib\pickle.py", line 433, in
save_reduce
save(state)
File "C:\Program Files\Python24\ lib\pickle.py", line 293, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Program Files\Python24\ lib\pickle.py", line 663, in
save_dict
self._batch_set items(obj.iteri tems())
File "C:\Program Files\Python24\ lib\pickle.py", line 677, in
_batch_setitems
save(v)
File "C:\Program Files\Python24\ lib\pickle.py", line 293, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Program Files\Python24\ lib\pickle.py", line 765, in
save_global
raise PicklingError(
pickle.Pickling Error: Can't pickle <function somefunc at 0x009EC5F0>:
it's not the same object as __
main__.somefunc
Pickling a function serializes the function name, but pickling a
staticmethod, classmethod, or instancemethod generates an error. In
these cases, pickle knows the instance or class, and the method, so
what's the problem? Pickle doesn't serialize code objects, so why can't
it serialize the name as it does for functions? Is this one of those
features that's feasible, but not useful, so no one's ever gotten
around to implementing it?
Regards,
Chris
>>import pickle
>>>
>>def somefunc():
>>>
>>def somefunc():
....
>>class Functions(objec t):
.... def somefunc():
.... return 1
....
>>class Foo(object):
....
>>f = Foo()
>>f.value = somefunc
>>print pickle.dumps(f)
>>f.value = somefunc
>>print pickle.dumps(f)
_reconstructor
p0
(c__main__
Foo
p1
c__builtin__
object
p2
Ntp3
Rp4
(dp5
S'value'
p6
c__main__
somefunc
p7
sb.
>>>
>>f.value = Functions.somef unc
>>print pickle.dumps(f)
>>f.value = Functions.somef unc
>>print pickle.dumps(f)
File "<stdin>", line 1, in ?
File "C:\Program Files\Python24\ lib\pickle.py", line 1386, in dumps
Pickler(file, protocol, bin).dump(obj)
File "C:\Program Files\Python24\ lib\pickle.py", line 231, in dump
self.save(obj)
File "C:\Program Files\Python24\ lib\pickle.py", line 338, in save
self.save_reduc e(obj=obj, *rv)
File "C:\Program Files\Python24\ lib\pickle.py", line 433, in
save_reduce
save(state)
File "C:\Program Files\Python24\ lib\pickle.py", line 293, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Program Files\Python24\ lib\pickle.py", line 663, in
save_dict
self._batch_set items(obj.iteri tems())
File "C:\Program Files\Python24\ lib\pickle.py", line 677, in
_batch_setitems
save(v)
File "C:\Program Files\Python24\ lib\pickle.py", line 293, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Program Files\Python24\ lib\pickle.py", line 765, in
save_global
raise PicklingError(
pickle.Pickling Error: Can't pickle <function somefunc at 0x009EC5F0>:
it's not the same object as __
main__.somefunc
Comment