Getting corresponding module from function

后端 未结 4 1233
深忆病人
深忆病人 2020-12-09 15:24

I want to modify a module xyz and its functions like that:

def modify(fun):
    modulename = fun.__module__ # this is string. ok, but not enough

import xyz
         


        
4条回答
  •  鱼传尺愫
    2020-12-09 16:27

    You want to get the module object from its name? Look it up in the sys.modules dictionary that contains all currently loaded modules:

    import sys
    
    def modify(func):
        module = sys.modules[func.__module__]
    

提交回复
热议问题