0

On Python, I made a module for saving and loading integers, It can save roughly as I want it (I am using Pickle) but when I load it I receive my integers in tuple-form (because I made it a tuple to save) I want to assign the components of the tuple to the integers in my program, but it won't please help! Here is my code:

def save(ob1,ob2,ob3,ob4,ob5):
    import pickle
    tmp = ob1,ob2,ob3,ob4,ob5
    output = open('save.sav','w')
    pickle.dump(tmp,output)
    output.close()

def load(ob1,ob2,ob3,ob4,ob5):
    import pickle
    input2 = open('save.sav','r')
    pickleload = pickle.load(input2)
    ob1 = pickleload[0]
    ob2 = pickleload[1]
    ob3 = pickleload[2]
    ob4 = pickleload[3]
    ob5 = pickleload[4]

I tried to do what aix said, but it didn't work. I am probably putting his code in the wrong places or something like that. Aix, could you please explain this better, or repost my code but edited? Or can someone else help me?

agf
  • 171,228
  • 44
  • 289
  • 238
  • Your title didn't provide any information about what your problem was. Another big problem with you not getting response was the tags -- "saving" and "loading" are totally generic and provide no information about the question. See the tags I added (especially "Python") and my rewrite of your title, for how to provide useful information to potential answerers. – agf Sep 21 '11 at 03:48
  • What does "but it doesn't" mean? What specifically happens? How are you calling `load` or `save`? If you're getting an error, post it! The full traceback, not just `ValueError` or whatever kind it is. – agf Sep 21 '11 at 03:49
  • I wasn't getting an error, it just wasn't working the way I wanted it to. – PythonPowerify Sep 21 '11 at 03:51

2 Answers2

1

Change load() like so:

def load():
    import pickle
    input2 = open('save.sav','r')
    return pickle.load(input2)

Then it can be used like so:

ob1, ob2, ob3, ob4, ob5 = load()

Your original version doesn't work because when you assign ob1 = ... inside the function, the change doesn't get propagated to the caller when the function returns (ob1 is an immutable object that gets passed by reference; assigning to it rebinds the reference, but the new reference doesn't get passed back to the caller).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I tried this just before, didn't work, don't know what I am doing wrong? Could you please repost my code, but edited in your fashion? – PythonPowerify Sep 20 '11 at 21:58
  • I'll also add that you "should" (though in CPython it doesn't really matter due to its refcounting GC) then close the file with `input2.close()` when done; this is easier done as `with open('sav.sav') as f: return pickle.load(f)`. (`r` is the default mode for `open()` so it can be dropped safely.) – Chris Morgan Sep 21 '11 at 04:51
0

The following Python code snippet should help you. The variables ob1 through ob5 are assigned the result of the pickle.load(open('save.sav', 'r')) The variables are declared global, so they can be accessed outside the def load(): function.

import pickle
def load():
    global ob1, ob2, ob3, ob4, ob5
    ob1, ob2, ob3, ob4, ob5 = pickle.load(open('save.sav', 'r'))
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Dima Tisnek
  • 11,241
  • 4
  • 68
  • 120