can't pickle instancemethod objects

Discussion in 'Python' started by Jim Lewis, Jul 9, 2006.

  1. Jim Lewis

    Jim Lewis Guest

    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?
     
    Jim Lewis, Jul 9, 2006
    #1
    1. Advertising

  2. On Sun, 09 Jul 2006 05:45:27 -0700, Jim Lewis wrote:

    > Pickling an instance of a class, gives "can't pickle instancemethod
    > objects". What does this mean?


    It means you can't pickle instance methods.

    > How do I find the class method creating the problem?


    How about you post the complete stack trace of the exception? Chances are
    it will contain much useful information.


    --
    Steven.
     
    Steven D'Aprano, Jul 9, 2006
    #2
    1. Advertising

  3. Jim Lewis

    Jim Lewis Guest

    > How about you post the complete stack trace of the exception?

    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\program files\python\lib\lib-tk\Tkinter.py", line 1345, in
    __call__
    return self.func(*args)
    File "C:\Public\world.py", line 1832, in BtnGo
    DoBtnGo()
    File "C:\Public\world.py", line 1812, in DoBtnGo
    if DoPickle: SavePickle ()
    File "C:\Public\world.py", line 1817, in SavePickle
    pickle.dump (pop,f)
    File "C:\program files\python\lib\pickle.py", line 1382, in dump
    Pickler(file, protocol, bin).dump(obj)
    File "C:\program files\python\lib\pickle.py", line 231, in dump
    self.save(obj)
    File "C:\program files\python\lib\pickle.py", line 293, in save
    f(self, obj) # Call unbound method with explicit self
    File "C:\program files\python\lib\pickle.py", line 739, in save_inst
    save(stuff)
    File "C:\program files\python\lib\pickle.py", line 293, in save
    f(self, obj) # Call unbound method with explicit self
    File "C:\program files\python\lib\pickle.py", line 663, in save_dict
    self._batch_setitems(obj.iteritems())
    File "C:\program files\python\lib\pickle.py", line 677, in
    _batch_setitems
    save(v)
    File "C:\program files\python\lib\pickle.py", line 293, in save
    f(self, obj) # Call unbound method with explicit self
    File "C:\program files\python\lib\pickle.py", line 614, in save_list
    self._batch_appends(iter(obj))
    File "C:\program files\python\lib\pickle.py", line 629, in
    _batch_appends
    save(x)
    File "C:\program files\python\lib\pickle.py", line 293, in save
    f(self, obj) # Call unbound method with explicit self
    File "C:\program files\python\lib\pickle.py", line 739, in save_inst
    save(stuff)
    File "C:\program files\python\lib\pickle.py", line 293, in save
    f(self, obj) # Call unbound method with explicit self
    File "C:\program files\python\lib\pickle.py", line 663, in save_dict
    self._batch_setitems(obj.iteritems())
    File "C:\program files\python\lib\pickle.py", line 677, in
    _batch_setitems
    save(v)
    File "C:\program files\python\lib\pickle.py", line 313, in save
    rv = reduce(self.proto)
    File "C:\program files\python\lib\copy_reg.py", line 69, in
    _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
    TypeError: can't pickle instancemethod objects
     
    Jim Lewis, Jul 9, 2006
    #3
  4. On Sun, 09 Jul 2006 07:06:25 -0700, Jim Lewis wrote:

    >> How about you post the complete stack trace of the exception?

    >
    > Exception in Tkinter callback
    > Traceback (most recent call last):
    > File "C:\program files\python\lib\lib-tk\Tkinter.py", line 1345, in
    > __call__
    > return self.func(*args)
    > File "C:\Public\world.py", line 1832, in BtnGo
    > DoBtnGo()
    > File "C:\Public\world.py", line 1812, in DoBtnGo
    > if DoPickle: SavePickle ()
    > File "C:\Public\world.py", line 1817, in SavePickle
    > pickle.dump (pop,f)


    I'd suggest that "pop" could be your culprit. At least, that's where I'd
    start looking. What is pop? A function or an instance method?

    I can't reproduce your error exactly -- the closest I get is "TypeError:
    can't pickle function objects" when I try to pickle a method. Possibly
    that's just a change in error message, which is not guaranteed to be
    constant across Python versions.

    --
    Steven.
     
    Steven D'Aprano, Jul 9, 2006
    #4
  5. Jim Lewis

    Jim Lewis Guest

    > I'd suggest that "pop" could be your culprit. ...What is pop? A function or an instance method?

    Neither. pop is an instance of a class, like:
    class X:
    ...
    pop = X ()

    pop surely is the culprit but it has arrays of objects, etc., and I
    don't know what to look for.
     
    Jim Lewis, Jul 9, 2006
    #5
  6. On Sun, 09 Jul 2006 08:39:29 -0700, Jim Lewis wrote:

    >> I'd suggest that "pop" could be your culprit. ...What is pop? A function or an instance method?

    >
    > Neither. pop is an instance of a class, like:
    > class X:
    > ...
    > pop = X ()
    >
    > pop surely is the culprit but it has arrays of objects, etc., and I
    > don't know what to look for.


    I'd start by looking for an attribute of pop that holds a reference to
    some function or method. E.g. something like this:

    class X():
    def method(self):
    pass
    def __init__(self):
    self.L = [1, "a", X.method] # note the lack of ()s

    pop = X()

    Otherwise, I'm working blind without knowing more about your class.

    Here's a thought: comment out every attribute in your class, and then try
    pickling it. If it succeeds, uncomment just *one* attribute, and try
    pickling again. Repeat until pickling fails.


    --
    Steven.
     
    Steven D'Aprano, Jul 9, 2006
    #6
  7. Jim Lewis

    Jim Lewis Guest

    > Here's a thought: comment out every attribute in your class, and then try
    > pickling it. If it succeeds, uncomment just *one* attribute, and try
    > pickling again. Repeat until pickling fails.


    Was trying to avoid that but you motivated me to do so and now I found
    the probem.

    In a utility routine I had:
    obj.act = act
    ActionSucceded = obj.act()

    Had to add:
    obj.act = None

    Thanks :)
     
    Jim Lewis, Jul 9, 2006
    #7
    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:
    347
    Terry Reedy
    Jul 28, 2003
  2. a pickle's pickle

    , Aug 2, 2005, in forum: Python
    Replies:
    4
    Views:
    394
  3. Steven Bethard

    pickle and instancemethod objects

    Steven Bethard, Sep 13, 2006, in forum: Python
    Replies:
    2
    Views:
    419
    Steven Bethard
    Sep 13, 2006
  4. Michele Simionato
    Replies:
    2
    Views:
    1,914
    Michele Simionato
    May 23, 2008
  5. est
    Replies:
    10
    Views:
    1,702
Loading...

Share This Page