0

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

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Joonho Park
  • 511
  • 5
  • 17
  • 1
    Just make a dictonary of dictionaries – Sayse Oct 25 '21 at 08:43
  • 1
    Possible duplicate: [How do I create variable variables?](https://stackoverflow.com/q/1373164/1324033) – Sayse Oct 25 '21 at 08:43
  • This is not duplicated following the correct answer from SomeoneRandom3124. This not the problem for the variable for variables. The question is different and the answer is different. – Joonho Park Oct 28 '21 at 07:46

3 Answers3

2

Using a dictionary of dictionaries would be much easier:

mega_dict = {'a': {'shape':1}, 'b' :{'opt':'sp'}}
print(mega_dict['a']['shape']) # or any other usage...
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

dictionary of dictionaries is more efficient but another possible solution to this is the use of eval

>>> dict_a = {'shape':1}
>>> dict_b = {'opt':'sp'}
>>> job = "dict_a"
>>> dic = eval(job)
>>> dic
{'shape': 1}
SR3142
  • 550
  • 3
  • 9
  • job should be "a" not "dict_a". – Joonho Park Oct 25 '21 at 10:03
  • 1
    you can replace job with `dictname` and that will give you what you need. note this returns a reference so if you modify `dic` you are actually modifying `dict_a` – SR3142 Oct 25 '21 at 12:37
  • This looks more fancy: job is a string so it can change. And by eval(job), if there is job-variable, the job value(string, dict, any), that is object, is referenced by dic. It looks the one exactly which I was thinking. There is no the secondary dict, that is dict of dicts, if I have followed correctly. – Joonho Park Oct 28 '21 at 07:20
  • I confirmed that this is working without making dict for dicts. – Joonho Park Oct 28 '21 at 07:40
  • The function of eval() looks valuable. Once I have asked a question whether there is a preprocessor function of C in python and some experts said No. But in my thought, it might be possible that eval() can replace the the function of preprocessor such as (#ifdef #else #endif) in python. – Joonho Park Oct 29 '21 at 02:28
1

You can create a dictionary consisting of all dictionaries. Something like this

final_dict = {'dict_a' : dict_a, 'dict_b' : dict_b}

Then in your running method, you can just simply do

dictname = 'dict_' + job
dic = final_dict[dictname]
Linux Geek
  • 957
  • 1
  • 11
  • 19