def load_attribute(attribute):
''' Loads or reloads the given attribute. For example:
1) load_attribute('module.submodule.class_name')
will return the type class_name (but not instantiate it)
from the module 'module.submodule'
2) load_attribute('module.submodule.func_name')
will return the function from that same module. '''
attribute_name = attribute.split('.')[-1]
pymodule = '.'.join(attribute.split('.')[:-1])
if pymodule in sys.modules:
pymodule_instance = importlib.reload(sys.modules[pymodule])
else:
pymodule_instance = importlib.import_module(pymodule)
# Get the attribute and return it
return getattr(pymodule_instance, attribute_name)
评论列表
文章目录