0

I intend to save all my class variables at the current stage and load them later for replicating the same work. I tried to pickle all the object instance of the class but I guess since I created another sub object in the target object, it cannot pickle. My another solution is to pickle objects "self" to reload all the class wide variables for later use. Is it viable through pickle or what do you suggest?

For instance I have

class A:
...

class B:
...
def __init__():
    self.A_ins = A
    self.var1 = ...
    self.var2 = ...

b = B()
f = open(file_name,'wb')
pickle.dump(b,f)

This is the error I got, if I try to pickle directly

TypeError: can't pickle instancemethod objects

In this example I try to save Class B object and reload it later. If it is not possible because of sub object A_ins, I propose to pickle self of class B object and reload it.

erogol
  • 13,156
  • 33
  • 101
  • 155

3 Answers3

1

It looks as though you're setting self.A_ins to the class A, not to an instance of class A. That could be your problem -- you're trying to pickle the __init__() method of A.

Try changing self.A_ins = A to self.A_ins = A().

kronosapiens
  • 1,333
  • 1
  • 10
  • 19
0

You can't pickle that with pickle, however if you use dill, this should not be a problem. dill should be able to pickle B regardless if have an instance of A or the class object A (as you have above).

Python 3.3.5 (default, Mar 10 2014, 21:37:38) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...   pass
... 
>>> class B:
...   def __init__(self):
...     self.A_ins = A 
...     self.var1 = 1
...     self.var2 = 2
... 
>>> b = B()
>>>   
>>> import dill
>>> dill.dumps(b)
b'\x80\x03cdill.dill\n_create_type\nq\x00(cdill.dill\n_load_type\nq\x01X\x04\x00\x00\x00typeq\x02\x85q\x03Rq\x04X\x01\x00\x00\x00Bq\x05h\x01X\x06\x00\x00\x00objectq\x06\x85q\x07Rq\x08\x85q\t}q\n(X\r\x00\x00\x00__slotnames__q\x0b]q\x0cX\n\x00\x00\x00__module__q\rX\x08\x00\x00\x00__main__q\x0eX\x08\x00\x00\x00__init__q\x0fcdill.dill\n_create_function\nq\x10(cdill.dill\n_unmarshal\nq\x11C\xb0c\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00C\x00\x00\x00s\x1f\x00\x00\x00t\x00\x00|\x00\x00_\x01\x00d\x01\x00|\x00\x00_\x02\x00d\x02\x00|\x00\x00_\x03\x00d\x00\x00S(\x03\x00\x00\x00Ni\x01\x00\x00\x00i\x02\x00\x00\x00(\x04\x00\x00\x00u\x01\x00\x00\x00Au\x05\x00\x00\x00A_insu\x04\x00\x00\x00var1u\x04\x00\x00\x00var2(\x01\x00\x00\x00u\x04\x00\x00\x00self(\x00\x00\x00\x00(\x00\x00\x00\x00u\x07\x00\x00\x00<stdin>u\x08\x00\x00\x00__init__\x02\x00\x00\x00s\x06\x00\x00\x00\x00\x01\t\x01\t\x01q\x12\x85q\x13Rq\x14c__builtin__\n__main__\nh\x0fNN}q\x15tq\x16Rq\x17X\x07\x00\x00\x00__doc__q\x18Nutq\x19Rq\x1a)\x81q\x1b}q\x1c(X\x05\x00\x00\x00A_insq\x1dh\x00(h\x04X\x01\x00\x00\x00Aq\x1eh\x08\x85q\x1f}q (h\x18Nh\rh\x0eutq!Rq"X\x04\x00\x00\x00var2q#K\x02X\x04\x00\x00\x00var1q$K\x01ub.'
>>> 
>>> _b = dill.loads(dill.dumps(b))
>>> _b.A_ins 
<class '__main__.A'>
>>> 
Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
0

If you are on Python2, you have classic classes, which are unpickelable. Try to inherit from object, i.e. replace class A: with class A(object):.

If you are on Python3 I guess you have a different problem, since newstyle is the default. https://docs.python.org/2/reference/datamodel.html#newstyle

EDIT:

Do not assume OP uses Python2

jarondl
  • 1,593
  • 4
  • 18
  • 27