473,287 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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
Jun 27 '08 #1
2 3486
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: greg.landrum | last post by:
Hi, I have a fairly sizable body of python code that I'm in the process of modernizing. One of the modernization steps is converting to use new-style classes, and this is leading to problems...
6
by: Jim Lewis | last post by:
Pickling an instance of a class, gives "can't pickle instancemethod objects". What does this mean? How do I find the class method creating the problem?
2
by: Steven Bethard | last post by:
I'd like to be able to pickle instancemethod objects mainly because I want to be able to delay a call like ``foo(spam, badger)`` by dumping ``foo``, ``spam`` and ``badger`` to disk and loading them...
5
by: Chris | last post by:
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...
3
by: fizilla | last post by:
Hello all! I have the following weird problem and since I am new to Python I somehow cannot figure out an elegant solution. The problem reduces to the following question: How to pickle a...
2
by: Mario Ceresa | last post by:
Hello everybody: I'd like to use the pickle module to save the state of an object so to be able to restore it later. The problem is that it holds a list of other objects, say numbers, and if I...
1
by: srinivasan srinivas | last post by:
Hi, I would like to know how to pickle a bound method?? Thanks, Srini Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/
1
by: srinivasan srinivas | last post by:
No. It does not work. def make_staticmethod(inst, methodname):     return getattr(inst, methodname) def pickle_function(method):     return make_staticmethod, (method.im_self,...
10
by: est | last post by:
>>import md5 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python25\lib\pickle.py", line 1366, in dumps Pickler(file, protocol).dump(obj) File...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.