0
我遇到了泡菜的問題,代碼是:不能鹹菜instancemethod對象
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)
錯誤是:
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
有如果你看一下'dill'在做什麼,它只是註冊序列化功能與'copyreg'來解決這個問題,而不是更換 – DemoRunner
ohter方式。如果你願意,你可以做同樣的...基本上,註冊'蒔蘿'使用相同的序列化函數,然後'pickle'將爲你工作,如上所述。 –