Everything below is from the main page of www.pythontutor.com (a fantastic tool and website by the way).
Here's some code
Here's what the author describes as the "global frame" and the "stack frames" at the current point of execution for the above code:
My question: What's the difference between "global frame" and the "stack frame"? Is this terminology even correct (I googled around and got all kinds of different answers)?
frames
are actual python objects that you can interact with:There is nothing particularly special about a global frame vs a local frame - they are just frames in the
stack
of execution:When ever you call a function (defined with python source code) it will add a frame for it's local execution to the stack, when ever a module is loaded a frame for the global execution of the module is added to the stack.
Frames don't have any standardized naming convention, so terminology accross the internet will probably contradicting. Usually you can identify them by the file and function name. Python refers to global frames as being a function named
<module>
as can be seen in above example (function='<module>'
) or in errors:The only real difference between "global" and "function" frames is that with global frames there is no distinction between global and local variables:
Which is why putting the
global
keyword in the global frame is meaningless, it indicates variable names that - when assigned - should be put in.f_globals
instead of.f_locals
. But other then that all frames are pretty much equal.