![]() |
pickle error: can't pickle instancemethod objects
Can somebody explain what's happening with the following script?
$ echo example.py import pickle class Example(object): def __init__(self, obj, registry): self._obj = obj self._registry = registry for name, func in self._registry.iteritems(): setattr(self, name, func.__get__(obj, obj.__class__)) def __gestate__(self): # should skip the bound methods attributes return dict(_registry=self._registry, _obj=self._obj) class C(object): pass def foo(self): pass if __name__ == '__main__': ex = Example(C(), dict(foo=foo)) pickle.dumps(ex) I get the following traceback: Traceback (most recent call last): File "pickle_error.py", line 22, in <module> pickle.dumps(ex) File "/usr/lib/python2.5/pickle.py", line 1366, in dumps Pickler(file, protocol).dump(obj) File "/usr/lib/python2.5/pickle.py", line 224, in dump self.save(obj) File "/usr/lib/python2.5/pickle.py", line 331, in save self.save_reduce(obj=obj, *rv) File "/usr/lib/python2.5/pickle.py", line 419, in save_reduce save(state) File "/usr/lib/python2.5/pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.5/pickle.py", line 649, in save_dict self._batch_setitems(obj.iteritems()) File "/usr/lib/python2.5/pickle.py", line 663, in _batch_setitems save(v) File "/usr/lib/python2.5/pickle.py", line 306, in save rv = reduce(self.proto) File "/usr/lib/python2.5/copy_reg.py", line 69, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle instancemethod objects I know that instancemethods cannot be pickled, this is why I used a __getstate__ method, but apparently it does not work. Any hint? Michele Simionato |
Re: pickle error: can't pickle instancemethod objects
Michele Simionato wrote:
> Can somebody explain what's happening with the following script? > def __gestate__(self): # should skip the bound methods attributes Must be __getstate__ ;) Peter |
Re: pickle error: can't pickle instancemethod objects
On May 23, 10:12*am, Peter Otten <__pete...@web.de> wrote:
> Michele Simionato wrote: > > Can somebody explain what's happening with the following script? > > * * def __gestate__(self): # should skip the bound methods attributes > > Must be __getstate__ ;) > > Peter Aaargh!!! I spent a couple of hours on a typo! Thanks anyway, now everything makes sense again ;) |
| All times are GMT. The time now is 03:54 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.