Last active
August 29, 2015 14:21
-
-
Save recall704/dcd59fa8b1ea1f5346c0 to your computer and use it in GitHub Desktop.
Getting the caller function name inside another function in Python?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment