[Tutor] Why doesn't it save the data before exiting? CORRECTION

Bob Gailer bgailer at alum.rpi.edu
Tue Jun 27 04:49:45 CEST 2006


Nathan Pinno wrote:

I already told you that the data is being saved to the file. And is 
being reloaded by ac = pickle.load(store). But the reloaded data is not 
being assigned to accountlist, since parameters to functions are treated 
as local variables. Assigning to a parameter in a function does NOT 
change the value of the parameter in the call.

You can demonstrate this as follows:
 >>> x = 2
 >>> def f(y):
...    y = 3
 >>> print x
2

To fix your program, define load_file as:

def load_file():
    import os
    import pickle
    filename = 'accounts.txt'
    if os.path.exists(filename):
        store = open(filename, 'r')
        ac = pickle.load(store)
        store.close()
    else:
        ac = {}
    return ac

And change load_file(accountlist) to:

accountlist = load_file()


[snip]

-- 

Bob Gailer
510-978-4454



More information about the Tutor mailing list