如何找到从Python导入函数的位置? [英] How to find where a function was imported from in Python?

查看:204
本文介绍了如何找到从Python导入函数的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有功能的Python模块:

I have a Python module with a function in it:

  == bar.py ==
  def foo(): pass
  == EOF ==

然后将其导入到全局名称空间中,如下所示:

And then I import it into the global namespace like so:

from bar import *

所以我现在可以使用功能foo.如果我打印出来:

So now the function foo is available to me. If I print it:

print foo

译员愉快地告诉我:

 <function foo at 0xb7eef10c>

在这一点上,我是否有办法找出功能foo来自模块bar?

Is there a way for me to find out that function foo came from module bar at this point?

推荐答案

foo.__module__应该返回bar

如果您需要更多信息,可以从sys.modules['bar']获取它,它的__file____package__属性可能很有趣.

If you need more info, you can get it from sys.modules['bar'], its __file__ and __package__ attributes may be interesting.

这篇关于如何找到从Python导入函数的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆