23ewrw 发表于 2016-3-17 08:26:10

python 根据路径和类名动态加载类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def get_plugin_by_name(self, package, clazz_name):
    """
    package 类所在路径
    clazz_name 类名
    http://stackoverflow.com/questions/547829/how-to-dynamically-load-a-python-class
    https://docs.python.org/2/library/functions.html?highlight=__import__#__import__
    :param package:
    :param clazz_name:
    :return:
    """
    try:
      plugin_name = clazz_name.capitalize() + 'Plugin'
      module = __import__(package + '.' + clazz_name, fromlist=)
      claz = getattr(module, plugin_name)
      return claz(region_id=self.get_current_region_id(),
                  access_key=self.aliyun_access_key)
    except Exception as exp:
      LOG.error("Get plugin: %s by package: %s error for %s" % (clazz_name, package, exp,))




页: [1]
查看完整版本: python 根据路径和类名动态加载类