Referring to class names through strings?

后端 未结 5 1063
面向向阳花
面向向阳花 2021-01-16 17:32

I need to parse some text file, create objects for various entities encountered in the text, and put them in some data structure (e.g., a list) for further processing. Examp

5条回答
  •  太阳男子
    2021-01-16 18:11

    you can create instances of a class by it's name using the following code

    obj = globals()[classname]()
    

    where classname is a string

提交回复
热议问题