Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Created February 18, 2019 08:37
Show Gist options
  • Save ayuLiao/7ccb12dc68620bf7dfc46fb30b49bd70 to your computer and use it in GitHub Desktop.
Save ayuLiao/7ccb12dc68620bf7dfc46fb30b49bd70 to your computer and use it in GitHub Desktop.
python获取调用方法的上层方法的名称
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