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

发布于 2024-07-25 22:44:56 字数 335 浏览 4 评论 0原文

假设 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”)的 /code> 属性。 如何才能做到这一点?

Suppose myapp/foo.py contains:

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

And myapp/bar.py contains:

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

I want caller_name to be set to the __name__ attribute of the calling functions' module (which is 'myapp.foo') in this case. How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心如荒岛 2024-08-01 22:44:56

查看检查模块:

inspect.stack() 将返回堆栈信息。

在函数内部,inspect.stack()[1] 将返回调用者的堆栈。 从那里,您可以获得有关调用者的函数名称、模块等的更多信息。

有关详细信息,请参阅文档:

http://docs.python.org/library/inspect.html

另外,Doug Hellmann 在他的 PyMOTW 系列中对检查模块有一篇很好的文章:

http://pymotw.com/2/inspect/index.html#module-inspect

编辑:这里有一些代码可以完成您的任务我想:

import inspect 

def info(msg):
    frm = inspect.stack()[1]
    mod = inspect.getmodule(frm[0])
    print '[%s] %s' % (mod.__name__, msg)

Check out the inspect module:

inspect.stack() will return the stack information.

Inside a function, inspect.stack()[1] will return your caller's stack. From there, you can get more information about the caller's function name, module, etc.

See the docs for details:

http://docs.python.org/library/inspect.html

Also, Doug Hellmann has a nice writeup of the inspect module in his PyMOTW series:

http://pymotw.com/2/inspect/index.html#module-inspect

EDIT: Here's some code which does what you want, I think:

import inspect 

def info(msg):
    frm = inspect.stack()[1]
    mod = inspect.getmodule(frm[0])
    print '[%s] %s' % (mod.__name__, msg)
葮薆情 2024-08-01 22:44:56

面对类似的问题,我发现 sys 模块中的 sys._current_frames() 包含有趣的信息,可以为您提供帮助,而无需导入检查,至少在特定用例中是这样。

>>> sys._current_frames()
{4052: <frame object at 0x03200C98>}

然后,您可以使用 f_back “向上移动”:

>>> f = sys._current_frames().values()[0]
>>> # for python3: f = list(sys._current_frames().values())[0]

>>> print f.f_back.f_globals['__file__']
'/base/data/home/apps/apricot/1.6456165165151/caller.py'

>>> print f.f_back.f_globals['__name__']
'__main__'

对于文件名,您也可以使用 f.f_back.f_code.co_filename,如上面 Mark Roddy 所建议的。 我不确定此方法的限制和注意事项(多线程很可能是一个问题),但我打算在我的情况下使用它。

Confronted with a similar problem, I have found that sys._current_frames() from the sys module contains interesting information that can help you, without the need to import inspect, at least in specific use cases.

>>> sys._current_frames()
{4052: <frame object at 0x03200C98>}

You can then "move up" using f_back :

>>> f = sys._current_frames().values()[0]
>>> # for python3: f = list(sys._current_frames().values())[0]

>>> print f.f_back.f_globals['__file__']
'/base/data/home/apps/apricot/1.6456165165151/caller.py'

>>> print f.f_back.f_globals['__name__']
'__main__'

For the filename you can also use f.f_back.f_code.co_filename, as suggested by Mark Roddy above. I am not sure of the limits and caveats of this method (multiple threads will most likely be a problem) but I intend to use it in my case.

感悟人生的甜 2024-08-01 22:44:56

我不建议这样做,但您可以通过以下方法完成您的目标:

def caller_name():
    frame=inspect.currentframe()
    frame=frame.f_back.f_back
    code=frame.f_code
    return code.co_filename

然后按如下方式更新现有方法:

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

I don't recommend do this, but you can accomplish your goal with the following method:

def caller_name():
    frame=inspect.currentframe()
    frame=frame.f_back.f_back
    code=frame.f_code
    return code.co_filename

Then update your existing method as follows:

def info(msg):
    caller = caller_name()
    print '[%s] %s' % (caller, msg)
因为看清所以看轻 2024-08-01 22:44:56

对于我来说,下面的行足以获取呼叫者的姓名。

import inspect
frame = inspect.stack()[-1]
print(frame.filename)

As for me, following line was enough to get callers'name.

import inspect
frame = inspect.stack()[-1]
print(frame.filename)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文