Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global frame vs. stack frame

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

like image 422
thanks_in_advance Avatar asked Aug 13 '16 23:08

thanks_in_advance


People also ask

What is the difference between stack and frame?

Every time you call another function, additional information is stored on the stack, including the return address (#1) and the values of local variables (#2). Each function-call's-worth of information is known as a "frame".

What is a global frame?

The module in which the program runs has the bottom-most frame which is called the global frame or the module frame. These frames keep the data that the function needs for its local execution which is the function arguments and its local variables.

What is meant by stack frames?

A stack frame is a memory management technique used in some programming languages for generating and eliminating temporary variables. In other words, it can be considered the collection of all information on the stack pertaining to a subprogram call.

When would you use a stack frame?

Stack is one of the segments of application memory that is used to store the local variables, function calls of the function. Whenever there is a function call in our program the memory to the local variables and other function calls or subroutines get stored in the stack frame.


1 Answers

TL DR:

when you call a function, a frame is created for the local execution.

When a module is loaded, a frame is created for the global module execution.

python tutor doesn't support showing execution across multiple files so the initial frame for the main module can seem like something unique, but you could imagine the statement import foo creating a new frame for the execution of the module foo just like calling functions creates frames for executing those functions.


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_code.co_filename) #filename of this code executing or '<pyshell#1>' etc.
print(my_frame.f_lineno) #this is line 9 so it prints 9

There is nothing particularly special about a global frame vs a local frame - 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 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 be 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:

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

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
True

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.

like image 107
Tadhg McDonald-Jensen Avatar answered Oct 22 '22 08:10

Tadhg McDonald-Jensen