By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

pickle error: can't pickle instancemethod objects

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Jun 27 '08 #2

P: n/a
On May 23, 10:12*am, Peter Otten <__pete...@web.dewrote:
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 ;)
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.