title: How to overwrite the dump/load methods in the pickle class - customizing pickling and unpickling - Python
body: <p>So far, what I've done is this:</p>
  
  <pre><code>import pickle
  
  class MyPickler(pickle.Pickler):
      def __init__(self, file, protocol=None):
          super(MyPickler, self).__init__(file, protocol)
  
  class MyUnpickler(pickle.Unpickler):
      def __init__(self, file):
          super(MyUnpickler, self).__init__(file) 
  </code></pre>
  
  <p>In my main method, this is mainly what I have</p>
  
  <pre><code>#created object, then... 
  pickledObject = 'testing.pickle'
  with open(pickledObject,'wb') as f:
      pickle = MyPickler(f)
      pickle.dump(object) #object is the object I want to pickle, created before this
  
  with open(pickledObject, 'r') as pickledFile:
      unpickle = MyUnpickler(pickledFile)
      object2 = unpickle.load()
  </code></pre>
  
  <p>However, this is giving me the following error when the super method is called:
  <code>TypeError: must be type, not classobj</code></p>
  
  <p>How does one overwrite only the two methods, load and dump? The pickle file is under C:\Python27/lib/pickle.py </p>
  
  <p><strong>EDIT</strong>
  The enum.py file can be found here: <a href="http://dpaste.com/780897/" rel="noreferrer">http://dpaste.com/780897/</a></p>
  
  <p>Object details:
  Object is initialized like this:</p>
  
  <pre><code>object = CellSizeRelation(CellSizeRelation.Values.FIRST)
  </code></pre>
  
  <p>And CellSizeRelation is a class that uses the Enumeration:</p>
  
  <pre><code>class CellSizeRelation(Option):
      Values = enum.Enum('FIRST',
                         'SECOND')
  </code></pre>
  
  <p>Before I pickle object, I do this:</p>
  
  <pre><code>print object.Values._values 
  print object.value.enumtype 
  </code></pre>
  
  <p><em>output</em></p>
  
  <pre><code>[EnumValue(&lt;enum.Enum object at 0x02E80E50&gt;, 0, 'FIRST'), EnumValue(&lt;enum.Enum object at 0x02E80E50&gt;, 1, 'SECOND')
  &lt;enum.Enum object at 0x02E80E50&gt;
  </code></pre>
  
  <p>After I unpickle and print out the same thing, I get this <em>output</em>:</p>
  
  <pre><code>[EnumValue(&lt;enum.Enum object at 0x02E80E50&gt;, 0, 'FIRST'), EnumValue(&lt;enum.Enum object at 0x02E80E50&gt;, 1, 'SECOND')
  &lt;enum.Enum object at 0x02ECF750&gt;
  </code></pre>
  
  <p>The problem is that the second object address changes; When initialized the first time, the <code>enumtype</code> and <code>_values</code> have the same address. However, after unpickling, they change addresses. This breaks my code when I try to compare two enumValues. If you look in the <code>enumValue</code> class, the compare function tries to do this:</p>
  
  <pre><code>try:
          assert self.enumtype == other.enumtype
          result = cmp(self.index, other.index)
  </code></pre>
  
  <p>Because the address changes, the assert function fails. 
  I now somehow need to ensure that the address for the enumtype does not change when unpickled. I was thinking of simply getting the value 'FIRST' from the unpickled file, finding out its index, and reinitializing the object with:</p>
  
  <pre><code>def load:
      object = CellSizeRelation(CellSizeRelation.Values[INDEX])
      return object
  </code></pre>