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

后端 未结 7 1664
我寻月下人不归
我寻月下人不归 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:49

    This is often referred to as reflection or sometimes introspection. Check out a similar questions that have an answer for what you are trying to do:

    Does Python Have An Equivalent to Java Class forname

    Can You Use a String to Instantiate a Class in Python

提交回复
热议问题