如何实例化一个给定其名称为字符串的类?

这个问题在这里已经有了答案:

  • 通过使用其名称(字符串)调用模块的功能10个答案

  • 考虑到Python命名方案基于地图,即使使用eval ,您也将使用这种或那种地图。

    构建你自己的映射是最合适的,但是如果你的类存储在别的地方,你也可以使用globals()或者从模块中检索它:

    import util_module
    getattr(util_module, 'A') ()
    
    链接地址: http://www.djcxy.com/p/55177.html

    上一篇: How to instantiate a class given its name as a string?

    下一篇: Calling unspecified function from another file and another class