I am currently have an global dictionary variable
_vars ={
"key_1":"system environmental variable_1",
"key_2":"system environmental variable_2",
................
#around eight key pairs
}
The dictionary _vars will be initialized when the program runs by calling the initalize_vars function
def initialize_vars():
var key in _vars:
_vars[key] = os.path.expandvars(vars[key])
However, in the main function based on the number of arguments user provide, different functions will be called and some of them does not need to initialize the _vars (it will raise exception as the system environmental variable does not exist)
for example:
def A():
initialize_vars()
#do something
return something
def B():
#do something
return something
def C():
initialize_vars()
#do something
def main():
if sys.argv[1]=="A":
A()
elif sys.argv[1] =="B":
B()
elif sys.argv[1] =="C":
C()
So my question is: what is the best way to ensure that initialize_vars() does not called multiple times if it has been initialized already