My requirement is to use variable value for referncing class/dictionaries in Python. As a sample example, I have following data :-
class test1:
pass
class test2:
pass
test1_dict = {}
test2_dict = {}
testvariable = "test1"
Now I want to check value of testvariable
and create an instance of class and append it in dictionary.
e.g.
if testvariable == "test1":
test1inst = test1()
test1_dict["test1"] = test1inst
elif testvariable == "test2":
test2inst = test2()
test2_dict["test2"] = test2inst
In the above code, I have to explicitly use if/else
to check the value of testvariable
and do the operations accordingly.
In my real scenario, I could have multiple values of testvariable
and there could be multiple places where if/else
check would be required. So, is it possible that somehow, I could be able to use the value of testvariable
directly to refer dictionary/class instances without using if/else
.