Skip to content

Instantly share code, notes, and snippets.

@wungad
Last active January 25, 2020 09:54
Explaining frames to my dad
import inspect
def target():
# currentframe() is needed only for getouterframes() to
# have a reference where in the stack to start.
# Meaning this is True: cfr == ofr[0][0]
cfr = inspect.currentframe() # class FrameType
ofr = inspect.getouterframes(cfr) # List[FrameInfo, FrameInfo]
# FrameInfo has the following list-like structure:
# [ class FrameType, str filename, int lineno, str function,
# List[str code_context], int index ]
print(f'[frame]: i am func {ofr[0][3]}, born in line {ofr[0][2]}')
print(f'[frame]: i got called by func {ofr[1][3]} from line {ofr[1][2]}')
target()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment