itlynn
V2EX  ›  问与答

请问 Python 动态调用函数(类似插件)的一个问题

  •  1
     
  •   itlynn · Jun 3, 2016 · 1404 views
    This topic created in 3738 days ago, the information mentioned may be changed or developed.

    主文件 main.py ,其中有一个类,类中有一个方法 get_content()
    主文件中动态加载了某个 py 文件并执行返回,代码如下

    def run_module(module,mode,proxy):
        ''' 该模块接收一个字符串,作为函数名执行并返回执行结果'''
        
        #引入 plugins 文件夹下的 module 模块
        run_module = __import__('plugins',globals(),locals(),[module])
        
        #执行 module 模块中的 run 函数
        return getattr(run_module,module).run(mode,proxy)
    

    问题:我想在 module 模块中的 run 函数调用 main.py 中的 get_content()函数(plugins 中有大量的 module),有什么好方法?

    2 replies    2016-06-04 12:02:14 +08:00
    Victor215
        1
    Victor215  
       Jun 4, 2016 via Android
    完全没看懂你要表达什么意思……
    itlynn
        2
    itlynn  
    OP
       Jun 4, 2016 via iPhone
    @Victor215 Sorry.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2692 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 10:52 · PVG 18:52 · LAX 03:52 · JFK 06:52
    ♥ Do have faith in what you're doing.