title: How do I save and restore multiple variables in python?
body: <p>I need to save about a dozen objects to a file and then restore them later.
  I've tried to use a for loop with pickle and shelve but it didn't work right.</p>
  <p>Edit.<br />
  All of the objects that I was trying to save were in the same class (I should have mentioned this before), and I didn't realize that I could just save the whole class like this:</p>
  <pre><code>import pickle
  def saveLoad(opt):
      global calc
      if opt == &quot;save&quot;:
          f = file(filename, 'wb')
          pickle.dump(calc, f, 2)
          f.close
          print 'data saved'
      elif opt == &quot;load&quot;:
          f = file(filename, 'rb')
          calc = pickle.load(f)
      else:
          print 'Invalid saveLoad option'
  </code></pre>