user1883050 user1883050 - 1 year ago 81
Python Question

global frame vs. stack frame

Everything below is from the main page of (a fantastic tool and website by the way).

Here's some code (image file):

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:

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)?

Answer Source

frames are actual python objects that you can interact with:

import inspect

my_frame = inspect.currentframe()

print(my_frame) #<frame object at MEMORY_LOCATION>

print(my_frame.f_lineno) #this is line 7 so it prints 7
print(my_frame.f_lineno) #this is line 8 so it prints 8
print(my_frame.f_lineno) #this is line 9 so it prints 9

You seem to think that there is something special about a global frame vs a local frame but they are just frames in the stack of execution:

Python 3.6.0a1 (v3.6.0a1:5896da372fb0, May 16 2016, 15:20:48) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> import pprint
>>> def test():
...     pprint.pprint(inspect.stack())
>>> test() #shows the frame in test() and global frame
[FrameInfo(frame=<frame object at 0x1003a3be0>, filename='<stdin>', lineno=2, function='test', code_context=None, index=None),
 FrameInfo(frame=<frame object at 0x101574048>, filename='<stdin>', lineno=1, function='<module>', code_context=None, index=None)]
>>> pprint.pprint(inspect.stack()) #only shows global frame
[FrameInfo(frame=<frame object at 0x1004296a8>, filename='<stdin>', lineno=1, function='<module>', code_context=None, index=None)]

When ever you call a function (defined with python source code) it will add a frame local to that function call to the stack, when ever a module is loaded then the global frame for that module is added to the stack. This means that the first module loaded is always the bottom frame in the stack.

The only real difference between global and "function" frames is that with global frames there is no distinction between global and local variables:

>>> my_frame.f_globals is my_frame.f_locals

The global keyword indicates variables that - when assigned - should be set in .f_globals instead of .f_locals. This is why adding the global keyword in the global scope does nothing, because f_locals and f_globals point to the same namespace.

In your case the term "global frame" is just the name given to that frame because usually frames are identified by the function they are in, since global frames are not within a function they are just called "global" in your tutorial, python instead refers to them as being <module> as can be seen in above example (function='<module>') or in errors:

>>> raise TypeError
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    raise TypeError
                               # ^ up there