Link to home
Start Free TrialLog in
Avatar of Member_4228183
Member_4228183

asked on

Problem recalling dictionary outside of def

I am trying to recall a dictionary from a file which works fine only within the def DictionaryLoad() that I use to recall the file with. If I type in "Print UserData" within def DictionaryLoad() I get the UserData dictionary with the saved data. But If I recall it again later on (like I have in the code snippet) it only recalls the blank template of UserData. How do I fix this so that I can recall it later on in the code.
import pickle
 
UserData = {"Something": " ", "SomethingElse": " "}
 
def DictionaryLoad():
	file = open("UserData.dat", "r")
	UserData = pickle.load(file) #This works fine and I can recall UserData any were after this line in the def DictionaryLoad() area and it will have the saved data loaded from the UserData.dat file.
	file.close()
 
DictionaryLoad()
 
print UserData #only loads the blank UserData template here

Open in new window

Avatar of ghostdog74
ghostdog74

you need to return the value


 
def DictionaryLoad():
        file = open("UserData.dat", "r")
        UserData = pickle.load(file) #This works fine and I can recall UserData any were after this line in the def DictionaryLoad() area and it will have the saved data loaded from the UserData.dat file.
        file.close()
        return UserData
 
result = DictionaryLoad()
print result

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
RichieHindle: Oh, you should not tell him about the global at all :)))

No, just kidding ;) You should avoid using the global if possible for the same reasons as in other languages. As every value is passed by reference in Python, there is also a very small penalty for explicit passing (no cost of implicit copying).
Avatar of Member_4228183

ASKER

Why should I not use global? It seems to be the essayist way to do what I want to do.
ZoeticNepenthe: "Why should I not use global?"

Look at the core code of your module (ignoring the initialisation and the function definitions) using 'global' vs. using pepr's idea of returning the new dictionary:

# Using 'global':
DictionaryLoad()
print UserData

vs.

# Returning the new value (pepr's suggestion):
UserData = DictionaryLoad()
print UserData

In pepr's case, you can clearly see where UserData is coming from without having to go off and look at the definition of DictionaryLoad().  With the 'global' solution, it's not clear whether DictionaryLoad() will have any effect on UserData or not.

In a small script this probably doesn't matter very much, but in a larger application it can make a huge difference.
So there is nothing significantly wrong with using global but when you return the new value like pepr suggested it is easier to keep your code clean and define variables.
The problem with using global variables (also in other languages) may not be apparent in simple cases. The problem start to be tough when the global variable is shared by more functions.

When a function relies on status of a global variable or if it changes its content it influences behaviour of the other functions that also work with the global variable. The global variable makes the functions mutually dependent. When you add one more such function, you will add another piece of code that makes things even more dependent...

The problem is that it is difficult to design program that uses the functions in such a way that would produce correct results. Using the functions in slightly different order can cause problems that are difficult to debug because you, for example, did not think hard enough about the influence of the newly introduced function.
ok, thank you all for your help. ^_^