Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pirate
Created July 9, 2019 00:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirate/26da5975bc69f55b67b35151dc727a4e to your computer and use it in GitHub Desktop.
Save pirate/26da5975bc69f55b67b35151dc727a4e to your computer and use it in GitHub Desktop.
Efficiently return the full module path and function name of the calling function in Python e.g. "app.module.class.method"
import sys
def current_function_name(above=1):
"""returns the calling function's __module__.__name__"""
# https://gist.github.com/JettJones/c236494013f22723c1822126df944b12#gistcomment-2962311
frame = sys._getframe()
for frame_idx in range(0, above):
frame = frame.f_back
caller_module = frame.f_globals["__name__"]
caller_name = frame.co_code.co_name
return f'{caller_module}.{caller_name}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment