pickle error: can't pickle instancemethod objects

Discussion in 'Python' started by Michele Simionato, May 23, 2008.

  1. 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
    Michele Simionato, May 23, 2008
    #1
    1. Advertising

  2. Michele Simionato

    Peter Otten Guest

    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
    Peter Otten, May 23, 2008
    #2
    1. Advertising

  3. On May 23, 10:12 am, Peter Otten <> 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 ;)
    Michele Simionato, May 23, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rim
    Replies:
    7
    Views:
    331
    Terry Reedy
    Jul 28, 2003
  2. Martin Miller
    Replies:
    1
    Views:
    342
    Peter Hansen
    Mar 12, 2005
  3. a pickle's pickle

    , Aug 2, 2005, in forum: Python
    Replies:
    4
    Views:
    372
  4. Jim Lewis

    can't pickle instancemethod objects

    Jim Lewis, Jul 9, 2006, in forum: Python
    Replies:
    6
    Views:
    1,283
    Jim Lewis
    Jul 9, 2006
  5. Steven Bethard

    pickle and instancemethod objects

    Steven Bethard, Sep 13, 2006, in forum: Python
    Replies:
    2
    Views:
    398
    Steven Bethard
    Sep 13, 2006
Loading...

Share This Page