How can I fetch the caller object from within a function, and inspect information about that caller?
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()
bignose
32.7k16 gold badges81 silver badges116 bronze badges
asked Oct 21, 2012 at 12:10
user1559873
6,9068 gold badges29 silver badges29 bronze badges
-
5Maybe this is helpful. Google 'python get caller' turns up some nice results.BrtH– BrtH2012年10月21日 12:16:11 +00:00Commented Oct 21, 2012 at 12:16
-
What are you trying to do? It's usually not a good idea to do this.phant0m– phant0m2012年10月21日 12:57:40 +00:00Commented Oct 21, 2012 at 12:57
-
@phant0m i am trying to develop customized logger Framework,which fetch the object information as shown above.user1559873– user15598732012年10月21日 14:12:22 +00:00Commented Oct 21, 2012 at 14:12
2 Answers 2
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()
answered Aug 16, 2021 at 8:44
AXO
9,2466 gold badges73 silver badges69 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
David Jones
there is nothing that says your caller must have a local called self. that's pure convention (for methods not functions)
AXO
@DavidJones I changed my answer, however there are more complicated situations, e.g. for a static method. In the end one may need to analyze the whole source code to properly identify the calling object.
Not all of those are possible, but most are available by inspecting the call stack:
import sys
import inspect
__metaclass__ = type
class Lorem:
def ipsum(self):
caller_frame = sys._getframe(1)
caller_frameinfo = inspect.getframeinfo(caller_frame)
print("Caller is in module {module!r}".format(
module=inspect.getmodule(caller_frame)))
print("Caller is defined in {path!r}, line {lineno}".format(
path=inspect.getsourcefile(caller_frame),
lineno=caller_frameinfo.lineno))
class Dolor:
def sit_amet(self):
lorem = Lorem()
lorem.ipsum()
For more information, see the documentation for the inspect module.
answered Jul 1, 2015 at 8:05
bignose
32.7k16 gold badges81 silver badges116 bronze badges
1 Comment
Géry Ogam
What about the caller class?
lang-py