[英]Using variable value as dictionary/class name in Python
My requirement is to use variable value for referncing class/dictionaries in Python. 我的要求是使用变量值引用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. 现在,我想检查
testvariable
值并创建一个class实例,并将其附加到字典中。
eg 例如
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. 在上面的代码中,我必须显式地使用
if/else
来检查testvariable
的值并进行相应的操作。
In my real scenario, I could have multiple values of testvariable
and there could be multiple places where if/else
check would be required. 在我的实际场景中,我可能有多个
testvariable
值,并且可能在多个地方需要if/else
检查。 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
. 因此,是否有可能,我能够直接使用
testvariable
的值来引用字典/类实例,而无需使用if/else
。
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.
Python具有一个很好的数据结构,用于将名称映射到对象,这是一个决定。 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])
I agree with Daniel Roseman that there is almost never a good reason to do this. 我同意丹尼尔·罗斯曼(Daniel Roseman)的观点, 几乎从来没有理由这样做。 However, I'm up for a challenge!
但是,我正面临挑战! The OP follows my lead at his or her own peril.
OP跟随我的行为,后果自负。
The secret is to use Python's exec function, which permits executing the contents of a string as Python code: 秘诀是使用Python的exec函数,该函数允许将字符串的内容作为Python代码执行:
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. 注意:在OP的情况下,testvariable的其他值不起作用,在使用exec()的情况下,会导致NameError异常。
I'm going to combine some other posts and say that Python already has a dictionary that maps names of objects to the object. 我将结合其他一些文章,说Python已经有一个字典,将对象的名称映射到该对象。 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]()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.