Created
February 18, 2019 08:37
-
-
Save ayuLiao/7ccb12dc68620bf7dfc46fb30b49bd70 to your computer and use it in GitHub Desktop.
python获取调用方法的上层方法的名称
This file contains hidden or 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
import traceback | |
def fun(): | |
# 代码栈,其中包含调用该方法的方法名称 | |
s = traceback.extract_stack() | |
print '%s Invoked me!'%s[-2][2] | |
>>> def a():fun() | |
>>> def b():fun() | |
>>> a() | |
a Invoked me! | |
>>> b() | |
b Invoked me! | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment