Skip to content

Instantly share code, notes, and snippets.

@westc
Created May 11, 2020 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/605322f68ad487ded59ebbd4371f5aef to your computer and use it in GitHub Desktop.
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.
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