Last active
January 25, 2020 09:54
Explaining frames to my dad
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 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