get current function name inside that function using python
You probably want inspect.getframeinfo(frame).function
:
import inspectdef whoami(): frame = inspect.currentframe() return inspect.getframeinfo(frame).functiondef foo(): print(whoami())foo()
prints
whoami
Actually, Eric's answer points the way if this is about logging:
For my logging purpose i want to log all the names of functions where my code is going
You can adjust the formatter to log the function name:
import logging def whoami(): logging.info("Now I'm there")def foo(): logging.info("I'm here") whoami() logging.info("I'm back here again")logging.basicConfig( format="%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s", level=logging.INFO)foo()
prints
2015-10-16 16:29:34,227 [INFO] foo: I'm here2015-10-16 16:29:34,227 [INFO] whoami: Now I'm there2015-10-16 16:29:34,227 [INFO] foo: I'm back here again
For my logging purpose i want to log all the names of functions where my code is going
Have you considered decorators?
import functoolsdef logme(f): @functools.wraps(f) def wrapped(*args, **kwargs): print(f.__name__) return f(*args, **kwargs) return wrapped@logmedef myfunction(): print("Doing some stuff")