如何从函数中获取调用者对象,并检查有关该调用者的信息?
class A(object):
def class_A_fun(self):
print 'caller from class' # → B
print 'caller from method' # → class_B_fun
print 'caller module' # → foomodule
print 'caller instance' # → obj
print 'caller object file name or path' # → 'foomodule.py'
class B(object):
def class_B_fun(self):
obj = A()
obj.class_A_fun()
if __name__ == "__main__":
obj = B()
obj.class_B_fun()
最佳答案
from inspect import currentframe, getframeinfo, getmodulename
from pathlib import Path
class A(object):
def class_A_fun(self):
cuurent_frame = currentframe()
caller_frame = cuurent_frame.f_back
filename, lineno, function, code_context, index = getframeinfo(caller_frame)
# first_arg_name in this case is 'self', but could be sth else
first_arg_name = caller_frame.f_code.co_varnames[0]
# f_locals is the local namespace seen by the frame
caller_instance = caller_frame.f_locals[first_arg_name]
assert caller_instance is caller_frame.f_back.f_locals['b'] is b
assert type(caller_instance).__name__ == 'B'
assert function == caller_frame.f_code.co_name == 'class_B_fun'
assert filename == caller_frame.f_code.co_filename == __file__
assert getmodulename(filename) == Path(__file__).stem
class B(object):
def class_B_fun(self):
a = A()
a.class_A_fun()
if __name__ == "__main__":
b = B()
b.class_B_fun()
关于python - 如何获取python调用者对象信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12997687/