Reputation: 65
So I'm trying to make a saveload function using the shelve module. This is my code so far.
def sl(value=None,var,loadvar=None,s_l):
global loadvar
gamedata=shelve.open('game')
if s_l=='s':
gamedata[var]=value
gamedata.close()
if s_l=='l':
loadvar=gamedata.get(var)
gamedata.close()
So how do I get the function to give the value that it gets in this lineloadvar=gamedata.get(var)
to the variable outside the function(the variable name is entered as loadvar)
For example there is a variable named as variable1 and i have a value stored in the gamedata file under the name v.
I then run the function as follows:
sl(v,variable1,l)
Now the value of variable1 should be equal to v. How do i do this?
Upvotes: 0
Views: 47
Reputation: 332
There are two ways:
returning to outsite the function using return loadvar
and outside the function
variable = sl(v,variable1,l)
you can use globals()
def sl(value=None,var,loadvar=None,s_l):
global loadvar
gamedata=shelve.open('game')
if s_l=='s':
gamedata[var]=value
gamedata.close()
if s_l=='l':
globals()['loadvar']=gamedata.get(var)
gamedata.close()
then outside the function after its call you can use loadvars
anywhere
Upvotes: 1