DemoRunner
DemoRunner

Reputation: 53

can't pickle instancemethod objects

I met a problem of pickle, Code is that:

import cPickle

class A(object):

    def __init__(self):
        self.a = 1

    def methoda(self):
        print(self.a)


class B(object):

    def __init__(self):
        self.b = 2
        a = A()
        self.b_a = a.methoda

    def methodb(self):
        print(self.b)
if __name__ == '__main__':
    b = B()
    with open('best_model1.pkl', 'w') as f:
        cPickle.dump(b, f)

Error is that:

File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.name TypeError: can't pickle instancemethod objects

Upvotes: 4

Views: 7142

Answers (2)

Cobry
Cobry

Reputation: 4548

Also, when dill is installed pickle will work but as usual not cPickle.

import cPickle, pickle

class A(object):
    def __init__(self):
        self.a = 1
    def methoda(self):
        print(self.a)

class B(object):
    def __init__(self):
        self.b = 2
        a = A()
        self.b_a = a.methoda
    def methodb(self):
        print(self.b)

# try using cPickle
try:
    c = cPickle.dumps(b)
    d = cPickle.loads(c)
except Exception as err:
    print('Unable to use cPickle (%s)'%err)
else:
    print('Using cPickle was successful')
    print(b)
    print(d)

# try using pickle
try:
    c = pickle.dumps(b)
    d = pickle.loads(c)
except Exception as err:
    print('Unable to use pickle (%s)'%err)
else:
    print('Using pickle was successful')
    print(b)
    print(d)

 >>> Unable to use cPickle (can't pickle instancemethod objects)
 >>> Using pickle was successful
 >>> <__main__.B object at 0x10e9b84d0>
 >>> <__main__.B object at 0x13df07190>

for whatever reason, cPickle is not simply a C version of pickle 100 times faster but there are some differences

Upvotes: 0

Mike McKerns
Mike McKerns

Reputation: 35217

You can if you use dill instead of cPickle.

>>> import dill     
>>> 
>>> class A(object):
...   def __init__(self):
...     self.a = 1
...   def methods(self):
...     print(self.a)
... 
>>> 
>>> class B(object):
...   def __init__(self):
...     self.b = 2
...     a = A()
...     self.b_a = a.methods
...   def methodb(self):
...     print(self.b)
... 
>>> b = B()
>>> b_ = dill.dumps(b)
>>> _b = dill.loads(b_)
>>> _b.methodb()
2
>>> 

Also see: Can't pickle <type 'instancemethod'> when using python's multiprocessing Pool.map()

Upvotes: 3

Related Questions