如何在python中跟踪被调用方内部的调用方详细信息

如何在python中跟踪被调用方内部的调用方详细信息,python,Python,我从模块中另一个名为module1.py的类调用modulemodule2.py中的函数 例如: *module2.py* def test_envconfig(): #I have to get the name/parent base class details of caller to this function #print the caller function name is ? #print the location of the caller fu

我从模块中另一个名为module1.py的类调用modulemodule2.py中的函数

例如:

*module2.py*

def test_envconfig():
     #I have to get the name/parent base class details of caller to this function
     #print the caller function name is ?
     #print the location of the caller function is ?
     return "user"

*module1.py*

class MyClass(SomeOther):
    def process(self):
         module2.test_envconfig()
在上面的示例中,MyClass中的process方法调用module1中的test_envconfig函数。因此,我必须在这里获取有关调用方的详细信息MyClass->baseclass以及test_envconfig函数中的其他一些内容,包括基类层次结构的详细信息

那么,如何在calleetest_envconfig中获取调用函数MyClass->Somether的详细信息呢

编辑:执行此操作的原因:*

动态地说,该体系结构是为与多个项目共享而设计的。我们有大量的模块调用函数test_envconfig来激活环境。对于一些特定位置的模块,当环境用户调用test_envconfig时,应该将其激活,当环境“prod”调用同一个test_envconfig时,应激活来自其他位置的其他几个模块。根据模块的位置进行环境激活。test_envconfig负责通过必要的配置激活环境


请您帮助查找test_envconfig中的呼叫者位置和姓名详细信息好吗?

经过几次尝试后,我得到了答案

这是一个适合我的解决方案。欢迎你的建议

模块2.py

def test_envconfig():
     import inspect
     for frame, filename, line_num, func, source_code, source_index in inspect.stack():
         if 'tests/unittests/capital/' in filename:
             print 'The Test env is activated for %s' % (filename)
     return "user"

你为什么要这么做?最简单的方法是将其显式化-向test_envconfig添加一个obj参数,然后在MyClass中调用module2.test_envconfigself。这是一种动态操作,有很多模块调用test_envconfig。对于一些模块,我必须重定向到另一个调用方。因此,我必须在运行时在test_envConfig内获取调用方的详细信息。您能提供更多的上下文吗?这个函数的作用是什么?一般来说,您可以使用进行内省,但可能有更简单的方法来实现这一点。添加了更多描述一个更简单、更直观、更可配置的解决方案是将环境类型传递给test_envconfig。