utils.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:switchboard 作者: josefschneider 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号