The question is about to getting one of multiple data in a run time. There are several data such as
dict_a = {'shape':1}
dict_b = {'opt':'sp'}
dict_c = ...
From the function with job of 'a', 'b', 'c', etc. Rather than using just long repetitive coding such as
if job == 'a':
dic = dict_a
elif job == 'b':
dic = dict_b
...
I want to write a more smart coding. I have tried this with exec, but this does not work.
def running(job):
dictname = 'dict_'+job
var = 'dic'
exec("%s = %s" % (var, dictname))
if "dic" in locals():
print(f"True, {dic}")
"dic" seems to be made by exec() but cannot use dic. The error message goes as follows:
print(f"True, {dic}")
NameError: name 'dic' is not defined