C
Chris
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
....
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_reduce(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_setitems(obj.iteritems())
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.PicklingError: 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
.... return 1
........ @staticmethod
.... def somefunc():
.... return 1
........ pass
....
Traceback (most recent call last):ccopy_reg
_reconstructor
p0
(c__main__
Foo
p1
c__builtin__
object
p2
Ntp3
Rp4
(dp5
S'value'
p6
c__main__
somefunc
p7
sb.
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_reduce(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_setitems(obj.iteritems())
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.PicklingError: Can't pickle <function somefunc at 0x009EC5F0>:
it's not the same object as __
main__.somefunc