Code is as follows:
def create_dict():
my_dict = {}
return my_dict
create_dict()
print (my_dict)
To which I get:
NameError: name 'my_dict' is not defined
If I assign the returned dictionary like this:
my_dict = create_dict()
instead of the create_dict() line then it works but this script will be called multiple times so I don't want my_dict being overwritten each time I call it. What I want to be able to do is call the function creat_dict() once at the beginning to create the dictionary then just add/remove things as I see fit. If I keep calling the script then my_dict will always be overwritten with an empty dictionary.
I could do this:
if my_dict exists: #This bit is pseudo code
my_dict = {}
else:
pass
but I'm curious nonetheless whether it is possible to return a 'Global' dictionary from a function (that terminology may be off).