Created
May 11, 2020 10:55
-
-
Save westc/605322f68ad487ded59ebbd4371f5aef to your computer and use it in GitHub Desktop.
Gets the first function that was externally called. In other words get the name of the first function in the chain of calls that is not defined in this file.
This file contains 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 inspect | |
def getFirstExternallyCalledName() -> str: | |
""" | |
Gets the name of the first function called externally in a chain of | |
called functions within this file. | |
""" | |
# Loop through and get the first caller function not originating | |
# from this file. | |
frame = inspect.currentframe() | |
current_filename = frame.f_code.co_filename | |
while frame and current_filename == frame.f_code.co_filename: | |
caller_name = frame.f_code.co_name | |
frame = getattr(frame, 'f_back') | |
return caller_name | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment