title: Saving and Loading a class instance from file
body: <p>an essential part of my project is being able to save and load class instances to a file. For further context, my class has both a set of attributes as well as a few methods.</p>
  
  <p>So far, I've tried using <em>pickle</em>, but it's not working quite as expected. For starters, it's not loading the methods, nor it's letting me add attributes that I've defined initially; in other words, it's not really making a copy of the class I can work with.</p>
  
  <p>Relevant Code:</p>
  
  <pre><code>class Brick(object):
  
      def __init__(self, name, filename=None, areaMin=None, areaMax=None, kp=None): 
          self.name = name
          self.filename = filename
          self.areaMin = areaMin
          self.areaMax = areaMax
          self.kp = kp
          self.__kpsave = None
          if filename != None:
              self.__logfile = file(filename, 'w')
      def __getstate__(self):
          f = self.__logfile
          self.__kpsave = []
          for point in self.kp: 
              temp = (point.pt, point.size, point.angle, point.response, point.octave, point.class_id)
              self.__kpsave.append(temp)
          return (self.name, self.areaMin, self.areaMax, self.__kpsave,
                  f.name, f.tell())
      def __setstate__(self, state):
          self.value, self.areaMin, self.areaMax, self.__kpsave, name, position = state
          f = file(name, 'w')
          f.seek(position)
          self.__logfile = f
          self.filename = name
          self.kp = [] 
          for point in self.__kpsave:
              temp = cv2.KeyPoint(x=point[0][0], y=point[0][1], _size=point[1], _angle=point[2], _response=point[3],
                                  _octave=point[4], _class_id=point[5])
              self.kp.append(temp)
      def calculateORB(self, img):
          pass #I've omitted the actual method here
  </code></pre>
  
  <p>(There are a few more attributes and methods, but they're not relevant)</p>
  
  <p>Now, this class definition works just fine when creating new instances: I can make a new Brick with just the name, I can then set areaMin or any other attribute, and I can use pickle(cPickle) to dump the current instance to a file just fine (I'm using those getstate and setstate because pickle won't work with OpenCV's Keypoint elements).</p>
  
  <p>The problem comes, of course, when I do load the instance: using pickle load() I <em>can</em> load the instance from a file, and the values I set previously will be there (ie I can access areaMin just fine if I did set a value for it) but I can't access either methods or add values to any of the other attributes if I never changed their values. I've noticed that I don't need to <code>import</code> my class definition either if I'm simply pickling from a completely different source file.</p>
  
  <p>Since all I want to do is build a "database" of sorts from my class objects, what's the best way to approach this? I know something that <em>should</em> work is to simply write a <code>.Save()</code> method that writes a .py source file where I essentially create an instance of the class, so I can then <code>.Load()</code> which will do <code>exec</code> and <code>eval</code> as appropriate, however, this seems like the worst possible way to do this, so, how should I actually do this?</p>
  
  <p>Thanks.</p>