recall704
5/21/2015 - 5:31 AM

Getting the caller function name inside another function in Python?

Getting the caller function name inside another function in Python?

#coding:utf-8

import inspect

def f():
    print('i am function f')
    print('my parent function is',inspect.stack()[1][3])

def g():
    print('i am function g')
    f()

def k():
    print('i am function k')
    f()

g()
k()
i am function g
i am function f
('my parent function is', 'g')
i am function k
i am function f
('my parent function is', 'k')