How do I get the module instance for a class in Python?

后端 未结 3 2068
温柔的废话
温柔的废话 2021-01-17 12:54

I am learning Python, and as always, I\'m being ambitious with my starter projects. I am working on a plugin system for a community site toolkit for App Engine. My plugin

3条回答
  •  佛祖请我去吃肉
    2021-01-17 13:23

    How about Importing modules

    X = __import__(‘X’) works like import X, with the difference that you 1) pass the module name as a string, and 2) explicitly assign it to a variable in your current namespace.

    You could pass the module name (instead of 'X') and get the module instance back. Python will ensure that you don't import the same module twice, so you should get back the instance that you imported earlier.

提交回复
热议问题