I need to have a global dict with string as keys and function references as values. I don't want to write something similar to a switch, because I need to be able to fetch the keys list from another script. I tried to write a script looking like that :
GLOBAL_VARIABLE = {'id_1' : func_1,
'id_2' : func_2,
'id_3' : func_3
}
def get_id_list():
return GLOBAL_VARIABLE.keys()
def func_1(arg):
<do some stuff>
def func_2(arg):
<do some stuff>
def func_3(arg):
<do some stuff>
But when I do this, Python throws me an error "NameError: name 'func_1' is not defined". The only two solutions I could think of were:
Declare the global variable at the end of the file, but then it's really annoying if I have to edit it, I'd rather have this info as close as possible from the top of the file.
Declare a hackish function "get_global_variable" that (create and) return the exact same dict. It's working, but it's pretty ugly.
Is there a way to do some lazy declaration and/or what would be a more pythonic way to tackle this situation?