Metamorphosis
RSS feed

Python 如何调用上层模块中的函数?

假设 main.py 中有函数 foo,plugin 模块中有函数 bar ,则在 bar 中:

def bar():
    import inspect
    upperframe_foo = inspect.currentframe().f_back.f_locals['foo']

即可获得 main 中的函数 foo。如图:


但请不要这样做。
原发布于 https://www.zhihu.com/question/20065473/answer/18937495


published 18 Sep 2013
blog comments powered by Disqus