Python – get the information of calling function from called function

in a function

def fun():pass

How does the function

know who called it?In a C language, it seems difficult. But in python it’s very simple

import traceback
def fun():
      s =  traceback.extract_stack()
      print '%s Invoked me!'%s[-2][2]

the fun function will tell you who called it and print it out, so let’s try it:

>>> def a():fun()
>>> def b():fun()
>>> a()
a Invoked me!
>>> b()
b Invoked me!
>>>

Read More: