获取Python中调用函数模块的__name__

11 浏览
0 Comments

获取Python中调用函数模块的__name__

假设myapp/foo.py包含:

def info(msg):
    caller_name = ????
    print '[%s] %s' % (caller_name, msg)

myapp/bar.py包含:

import foo
foo.info('Hello') # => [myapp.bar] Hello

我希望在这种情况下,caller_name被设置为调用函数模块的__name__属性(即'myapp.foo')。如何实现这个目标?

0