0

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.

3
  • See here: stackoverflow.com/questions/452969/… Commented Aug 23, 2013 at 8:35
  • What is the error you are getting? Commented Aug 23, 2013 at 13:10
  • Got it, I need to use dict and its methods to manage data. Fixed it, thanks. Commented Aug 23, 2013 at 13:18

3 Answers 3

10

There is almost never a good reason to look up names like this. Python has a perfectly good data structure for mapping names to objects, and that is a dict. If you ever find yourself saying "I need a dynamic lookup of something", then a dict is the answer. In your case:

from collections import defaultdict
test_classes = {
    'test1': test1,
    'test2': test2
}
test_instances = defaultdict(list)
test_instances[testvariable].append(test_classes[testvariable])
Sign up to request clarification or add additional context in comments.

1 Comment

I agree with you, but I am facing problems implementing the same in my actual code. Please see my edit, which represents my actual problem more closely.
0

I agree with Daniel Roseman that there is almost never a good reason to do this. However, I'm up for a challenge! The OP follows my lead at his or her own peril.

The secret is to use Python's exec function, which permits executing the contents of a string as Python code:

So,

if testvariable == "test1":
    test1inst = test1()
    test1_dict["test1"] = test1inst
elif testvariable == "test2":
    test2inst = test2()
    test2_dict["test2"] = test2inst

becomes

exec("%sinst = %s()" % (testvariable, testvariable))
exec("%s_dict[testvariable] = %sinst" % (testvariable, testvariable))

albeit with the caveat that other values of testvariable do nothing in the OP's case, and in the case using exec() result in NameError exceptions.

3 Comments

No, bad idea. You can do the same by looking up the class in the local variables or global variables: inst = locals()[testvariable]
Very true, although this method doesn't require testing for from where in the scope it comes. Also, I think my answer made clear that it was a bad idea to do this. :)
Yeah, I just saw "exec" and went to a very dark place. :)
0

I'm going to combine some other posts and say that Python already has a dictionary that maps names of objects to the object. You can access local and global variables so as long as your class is defined in the module you can do:

my_inst[testvariable] = locals()[testvariable]()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.