Frame Objects — ELI5

Imagine you are reading a choose-your-own-adventure book. You are on page 47, and the book says “flip to page 82.” Before you flip, you stick your finger on page 47 so you can come back later.

Now imagine page 82 says “flip to page 115.” You stick another finger there. Pretty soon you have several fingers holding different places in the book.

Python does the same thing every time it calls a function. Each function call gets its own bookmark — a little notepad that tracks which line Python is on, what the local variables are, and where to go back to when the function finishes. That bookmark is called a frame object.

When function A calls function B, and function B calls function C, Python has three frames stacked up — one for each call. When C finishes, Python throws away C’s frame, picks up B’s frame, and continues where B left off.

This is why error messages show you a “traceback” — it is literally walking backward through the stack of bookmarks, showing you each function that was in progress when something went wrong.

One thing to remember: A frame is Python’s bookmark for a running function. It holds the current line, the local variables, and a pointer back to whatever called it. When the function ends, the bookmark gets tossed and Python picks up the previous one.

pythoncompiler-internalslanguage-implementation

See Also