How to get the caller's method name in the called method?


Python: How to get the caller’s method name in the called method?

Assume I have 2 methods:

def method1(self):
    a = A.method2()

def method2(self):

If I don’t want to do any change for method1, how to get the name of the caller (in this example, the name is method1) in method2?

Asked By: zs2020



inspect.getframeinfo and other related functions in inspect can help:

>>> import inspect
>>> def f1(): f2()
>>> def f2():
...   curframe = inspect.currentframe()
...   calframe = inspect.getouterframes(curframe, 2)
...   print('caller name:', calframe[1][3])
>>> f1()
caller name: f1

this introspection is intended to help debugging and development; it’s not advisable to rely on it for production-functionality purposes.

Answered By: Alex Martelli

Shorter version:

import inspect

def f1(): f2()

def f2():
    print 'caller name:', inspect.stack()[1][3]


(with thanks to @Alex, and Stefaan Lippen)

Answered By: Todd Owen

I’ve come up with a slightly longer version that tries to build a full method name including module and class. (rev 9cccbf)

# Public Domain, i.e. feel free to copy/paste
# Considered a hack in Python 2

import inspect

def caller_name(skip=2):
    """Get a name of a caller in the format module.class.method

       `skip` specifies how many levels of stack to skip while getting caller
       name. skip=1 means "who calls me", skip=2 "who calls my caller" etc.

       An empty string is returned if skipped levels exceed stack height
    stack = inspect.stack()
    start = 0 + skip
    if len(stack) < start + 1:
      return ''
    parentframe = stack[start][0]    

    name = []
    module = inspect.getmodule(parentframe)
    # `modname` can be None when frame is executed directly in console
    # TODO(techtonik): consider using __main__
    if module:
    # detect classname
    if 'self' in parentframe.f_locals:
        # I don't know any way to detect call from the object method
        # XXX: there seems to be no way to detect static method call - it will
        #      be just a function call
    codename = parentframe.f_code.co_name
    if codename != '<module>':  # top level usually
        name.append( codename ) # function or a method

    ## Avoid circular refs and frame leaks
    del parentframe, stack

    return ".".join(name)
Answered By: anatoly techtonik

This seems to work just fine:

import sys
print sys._getframe().f_back.f_code.co_name
Answered By: Augiwan

Bit of an amalgamation of the stuff above. But here’s my crack at it.

def print_caller_name(stack_size=3):
    def wrapper(fn):
        def inner(*args, **kwargs):
            import inspect
            stack = inspect.stack()

            modules = [(index, inspect.getmodule(stack[index][0]))
                       for index in reversed(range(1, stack_size))]
            module_name_lengths = [len(module.__name__)
                                   for _, module in modules]

            s = '{index:>5} : {module:^%i} : {name}' % (max(module_name_lengths) + 4)
            callers = ['',
                       s.format(index='level', module='module', name='name'),
                       '-' * 50]

            for index, module in modules:


            fn(*args, **kwargs)
        return inner
    return wrapper


def foo():
    return 'foobar'

def bar():
    return foo()

def baz():
    return bar()

def fizz():
    return baz()


output is

level :             module             : name
    3 :              None              : fizz
    2 :              None              : baz
    1 :              None              : bar
    0 :            __main__            : foo
Answered By: migpok35

I found a way if you’re going across classes and want the class the method belongs to AND the method. It takes a bit of extraction work but it makes its point. This works in Python 2.7.13.

import inspect, os

class ClassOne:
    def method1(self):

class ClassTwo:
    def method2(self):
        curframe = inspect.currentframe()
        calframe = inspect.getouterframes(curframe, 4)
        print 'nI was called from', calframe[1][3], 
        'in', calframe[1][4][0][6: -2]

# create objects to access class methods
classoneObj = ClassOne()
classtwoObj = ClassTwo()

# start the program
Answered By: Michael Swartz


#!/usr/bin/env python
import inspect

called=lambda: inspect.stack()[1][3]

def caller1():
    print "inside: ",called()

def caller2():
    print "inside: ",called()
if __name__=='__main__':


[email protected]:~/Documents$ python 
inside:  caller1
inside:  caller2
[email protected]:~/Documents$

I would use inspect.currentframe().f_back.f_code.co_name. Its use hasn’t been covered in any of the prior answers which are mainly of one of three types:

  • Some prior answers use inspect.stack but it’s known to be too slow.
  • Some prior answers use sys._getframe which is an internal private function given its leading underscore, and so its use is implicitly discouraged.
  • One prior answer uses inspect.getouterframes(inspect.currentframe(), 2)[1][3] but it’s entirely unclear what [1][3] is accessing.
import inspect
from types import FrameType
from typing import cast

def demo_the_caller_name() -> str:
    """Return the calling function's name."""
    # Ref:
    return cast(FrameType, cast(FrameType, inspect.currentframe()).f_back).f_code.co_name

if __name__ == '__main__':
    def _test_caller_name() -> None:
        assert demo_the_caller_name() == '_test_caller_name'

Note that cast(FrameType, frame) is used to satisfy mypy.

Acknowlegement: comment by 1313e for an answer.

Answered By: Asclepius

Hey mate I once made 3 methods without plugins for my app and maybe that can help you, It worked for me so maybe gonna work for you too.

def method_1(a=""):
    if a == "method_2":

    if a == "method_3":

def method_2():

def method_3():

Answered By: Freezy

You can use decorators, and do not have to use stacktrace

If you want to decorate a method inside a class

import functools

# outside ur class
def printOuterFunctionName(func):
def wrapper(self):
    print(f'Function Name is: {func.__name__}')
return wrapper 

class A:
  def foo():

you may remove functools, self if it is procedural

Answered By: Mahi

An alternative to sys._getframe() is used by Python’s Logging library to find caller information. Here’s the idea:

  1. raise an Exception

  2. immediately catch it in an Except clause

  3. use sys.exc_info to get Traceback frame (tb_frame).

  4. from tb_frame get last caller’s frame using f_back.

  5. from last caller’s frame get the code object that was being executed in that frame.

    In our sample code it would be method1 (not method2) being executed.

  6. From code object obtained, get the object’s name — this is caller method’s name in our sample.

Here’s the sample code to solve example in the question:

def method1():

def method2():
        raise Exception
    except Exception:
        frame = sys.exc_info()[2].tb_frame.f_back

    print("method2 invoked by: ", frame.f_code.co_name)

# Invoking method1


method2 invoked by: method1

Frame has all sorts of details, including line number, file name, argument counts, argument type and so on. The solution works across classes and modules too.

Answered By: Firelord
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.