Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the caller function name inside another function in Python? [duplicate]

If you have 2 functions like:

def A
def B

and A calls B, can you get who is calling B inside B, like:

def A () :
    B ()

def B () :
    this.caller.name
like image 935
Joan Venge Avatar asked May 22 '09 23:05

Joan Venge


People also ask

How do you call one function inside another function in Python?

In Python, it is possible to pass a function as a argument to another function. Write a function useFunction(func, num) that takes in a function and a number as arguments. The useFunction should produce the output shown in the examples given below.

Can you call a function within the same function Python?

Python also accepts function recursion, which means a defined function can call itself. Recursion is a common mathematical and programming concept. It means that a function calls itself.

Can you call a function within the same function?

Calling a function from within itself is called recursion and the simple answer is, yes.


5 Answers

You can use the inspect module to get the info you want. Its stack method returns a list of frame records.

  • For Python 2 each frame record is a list. The third element in each record is the caller name. What you want is this:

    >>> import inspect
    >>> def f():
    ...     print inspect.stack()[1][3]
    ...
    >>> def g():
    ...     f()
    ...
    >>> g()
    g
    

  • For Python 3.5+, each frame record is a named tuple so you need to replace

    print inspect.stack()[1][3]
    

    with

    print(inspect.stack()[1].function)
    

    on the above code.

like image 64
Ayman Hourieh Avatar answered Oct 15 '22 00:10

Ayman Hourieh


There are two ways, using sys and inspect modules:

  • sys._getframe(1).f_code.co_name
  • inspect.stack()[1][3]

The stack() form is less readable and is implementation dependent since it calls sys._getframe(), see extract from inspect.py:

def stack(context=1):
    """Return a list of records for the stack above the caller's frame."""
    return getouterframes(sys._getframe(1), context)
like image 43
Eric Avatar answered Oct 15 '22 02:10

Eric


Note (June 2018): today, I would probably use inspect module, see other answers

sys._getframe(1).f_code.co_name like in the example below:

>>> def foo():
...  global x
...  x = sys._getframe(1)
...
>>> def y(): foo()
...
>>> y()
>>> x.f_code.co_name
'y'
>>>  

Important note: as it's obvious from the _getframe method name (hey, it starts with an underscore), it's not an API method one should be thoughtlessly rely on.

like image 33
Piotr Findeisen Avatar answered Oct 15 '22 00:10

Piotr Findeisen


This works for me! :D

>>> def a():
...     import sys
...     print sys._getframe(1).f_code.co_name
...
>>> def b():
...     a()
...
...
>>> b()
b
>>>
like image 20
Alex Cory Avatar answered Oct 15 '22 01:10

Alex Cory


you can user the logging module and specify the %(funcName)s option in BaseConfig()

import logging
logging.basicConfig(filename='/tmp/test.log', level=logging.DEBUG, format='%(asctime)s | %(levelname)s | %(funcName)s |%(message)s')

def A():
    logging.info('info')
like image 29
mescalin Avatar answered Oct 15 '22 02:10

mescalin