how to dynamically create an instance of a class in python?

后端 未结 7 1655
我寻月下人不归
我寻月下人不归 2020-12-01 06:09

I have list of class names and want to create their instances dynamically. for example:

names=[
\'foo.baa.a\',
\'foo.daa.c\',
\'foo.AA\',
 ....
]

def save(c         


        
7条回答
  •  离开以前
    2020-12-01 06:41

    You can use the python builtin eval() statement to instantiate your classes. Like this:

    aa = eval(cName)()
    

    Notice!

    using eval is dangerous and is a key for lots of security risks based on code injections.

提交回复
热议问题