Created
July 9, 2019 00:02
-
-
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"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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