6

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
3
  • 5
    Maybe this is helpful. Google 'python get caller' turns up some nice results. Commented Oct 21, 2012 at 12:16
  • What are you trying to do? It's usually not a good idea to do this. Commented Oct 21, 2012 at 12:57
  • @phant0m i am trying to develop customized logger Framework,which fetch the object information as shown above. Commented Oct 21, 2012 at 14:12

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

there is nothing that says your caller must have a local called self. that's pure convention (for methods not functions)
@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.
1

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

1 Comment

What about the caller class?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.