Using the python inspect module, in a function, I would like to get the source code of the line that called that function.
So in the following situation:
def fct1():
# Retrieve the line that called me and extract 'a'
return an object containing name='a'
a = fct1()
I would like to retrieve the string "a = fct1()" in fct1
All I can do so far is to retrieve the code of the whole module with :
code = inspect.getsource(sys._getframe().f_back)
Please note that fct1() can be called many times in the main module.
Eventually, what I want is to retrieve the variable name "a" which is easy if I can get s = "a = fct1()" in fct1() :
a_name = s.split("=")[0].strip()