title: How to pickle an object of a class B (having many variables) that inherits from A, that defines __setstate__ and __getstate__
body: <p><strong>My problem is:</strong></p>
  
  <pre><code>class A(object):
      def __init__(self):
          #init
      def __setstate__(self,state):
          #A __setstate__ code here            
      def __getstate__(self):
          #A __getstate__ code here
          return state
  
  class B(A):
      def __init__(self):
          #creates many object variables here
  </code></pre>
  
  <p>A is from an external library.</p>
  
  <p><strong>Hard solution</strong></p>
  
  <p>This I would like to avoid</p>
  
  <p>When pickling B, pickle of course uses class A's <code>__setstate__</code>, <code>__getstate__</code> methods, so in order for pickle to work I should do something like this:</p>
  
  <pre><code>class B(A):
      def __init__(self):
          #creates many object variables here
  
      def __setstate__(self,state)
          A.__setstate__(self,state)
          #B __setstate__ code here
          #getting various variables from state for example
          self._a0 = state['a0']
          self._a1 = state['a1']
          #...
          self._a100 = state['a100']
          self._a101 = state['a101']
  
      def __getstate__(self):
          state = A.__getstate__(self)
          #B __setstate__ code here
          #filling state with various variables  
          #getting various variables from state for example
          state['a0'] =  self._a0
          state['a1'] =  self._a1
          #...
          state['a100'] =  self._a100
          state['a101'] =  self._a101           
          return state
  </code></pre>
  
  <p><strong>My question is:</strong></p>
  
  <p>How can I avoid defining <code>__setstate__</code> and <code>__getstate__</code> in B so that pickle does the job of pickling variables by itself? 
  All variables in B are of type that pickle may pickle(handle) by itself.
  So if B didn't inherit from A, it would be possible with good results:</p>
  
  <pre><code>b = B()
  path = 'path.temp'
  fout = open(path,'w')
  pickler = pickl.Pickler(fout)
  
  pickler.dump(b)
  fout.close()
  
  fin = open(path,'r')
  upickler = pickl.Unpickler(fin)
  b = unpickler.load()
  fin.close()
  #b has all variables
  </code></pre>
  
  <p><strong>The obvious solution</strong></p>
  
  <pre><code>class B(object):
      def __init__(self):
          #creates many object variables here
          a = A()            
  </code></pre>
  
  <p>However I would like B to inherit from A.
  Any idea how to solve this or at least automate pickling/unpickling variables in B?</p>
  
  <p><strong>The workaround solution:</strong></p>
  
  <p>As for automating pickling in the <strong>Hard Solution</strong></p>
  
  <p>Add to B a dictionary holding variables to pickle:</p>
  
  <pre><code>class B(A):
      __picklableObjects__ = {'_a0', '_a1', ... ,'_a101'}
  
      def __init__(self):
          #creates many object variables here
          A.__init__(self)
          self._a0 = ...
          ...
          self._a101 = ...
  
      @staticmethod
      def getPicklableObjects():
          return B.__picklableObjects__
  
      def __setstate__(self,state):
          A.__setstate__(self,state)
          for po in B.getPicklableObjects():
             __dict__[po] = state[po]
  
      def __getstate__(self):
          state = A.__getstate__(self)
          for po in B.getPicklableObjects():
              state[po] = copy.deepcopy(__dict__[po])
          return state
  </code></pre>
  
  <p>Any other ideas?</p>
  
  <p><strong>A's library:</strong></p>
  
  <p>Ok so for any of you interested A is graph_tool.Graph:
  <a href="http://projects.skewed.de/graph-tool/browser/src/graph_tool/__init__.py" rel="nofollow">A src code</a></p>
  
  <pre><code>line 786: class Graph(object)
  
  ...
  
  line 1517: __getstate__
  
  ...
  
  line 1533: __setstate__
  </code></pre>