For my logging purpose i want to log all the names of functions where my code is going
Does not matter who is calling the function , i want the the function name in which i declare this line
import inspect
def whoami():
return inspect.stack()[1][3]
def foo():
print(whoami())
currently it prints foo
, i want to print whoami
Actually, Eric's answer points the way if this is about logging:
You can adjust the formatter to log the function name:
prints
You probably want
inspect.getframeinfo(frame).function
:prints
Have you considered decorators?
Use
f_code.co_name
member of the stack frame returned bysys._getframe()
.For example, in a
whoami()
function,