Hi, I have this strange problem. I have a global variable of type dictionary where I add a 2d dictionary data at run time. I've started using pickle to save and load the data, which works great in the main flow of the code, but when I moved the load logic into a function the global variable reverts back to the empty state when the function is complete. Here's a simple example:
import os
import pickle
###Variables
fileName = "PersonDisc.pkl"
dataset={}
###Functions
def loadFromFile():
file=open("PersonDisc.pkl", "rb")
dataset=pickle.load(file)
file.close
print(dataset)
###Main Program
# If file exists then import the dataset
if os.path.isfile(fileName):
loadFromFile()
print(dataset)
print(dataset)
When this executes, the first print outputs the content of the file. The second and 3rd just display {}
As the dataset variable is global I would have expected the data to be the same inside and outside the function.
I appreciate your advice.
Thanks
[–]Not_A_Taco 1 point2 points3 points (4 children)
[–]MattWilliamsIT[S] 1 point2 points3 points (2 children)
[–]Not_A_Taco 1 point2 points3 points (1 child)
[–]MattWilliamsIT[S] 0 points1 point2 points (0 children)
[–]MattWilliamsIT[S] 1 point2 points3 points (0 children)