I have a function that returns a dictionary.
I would like to be able to access and work with that dictionary many times in the code, without the need to be calling the function that produces that dictionary every time. In other words, call the function once, but work with the dictionary that it returns many times.
So this way, the dictionary is constructed only once (and maybe stored somewhere?), but called and utilised many times in the script.
def function_to_produce_dict():
dict = {}
# something
# something that builds the dictionary
return dict
create_dict = function_to_product_dict()
# other code that will need to work with the create_dict dictionary.
# without the need to keep constructing it any time that we need it.
I have read other posts such as: Access a function variable outside the function without using `global`
But I am not sure that by declaring the dictionary as global withing the function_to_produce_dict() is going to make the dictionary accessible without having to build it every time, by calling the function again and again.
Is this possible?