frame type in python

Question:

In a python reference manual said that

A code block is executed in an execution frame. A frame contains some
administrative information (used for debugging) and determines where
and how execution continues after the code block’s execution has
completed.

and

Frame objects represent execution frames. They may occur
in traceback objects

But I don’t understanf how frame does work. How can I get an acces to a current frame object? When is frame object creating? Is the frame object created everytime when a code of new block is strarting to execute?

Asked By: user2889159

||

Answers:

These frames are representations of the stack frames created by function calls. You should not need to access them in normal programming. A new frame is indeed created every time a function is called, and destroyed when it exits or raises an uncaught exception. Since function calls can go many levels deep your program ends up with a bunch of nested stack frames, but it’s not good programming practice (unless you are writing a debugger or similar application) to mess about with the frames even though Python does make them available.

Answered By: holdenweb

It is important to understand frames in Python 3, especially if you maintain production code that is sensitive to memory usage, such as deep learning models.

Frames are objects that represent areas of memory where local variables are stored when a function is called. They can be stored and manipulated, which is what debuggers do. Understanding how frames are handled by python can help you avoid memory leaks.

Each time a function is called, a new frame is created to hold the function’s variables and parameters. These frames are normally destroyed when the function finishes executing normally. However, if an exception is raised, the associated frame and all parent frames are stored in a traceback object, which is an attribute of the Exception object (__traceback__). This can cause memory leaks if the Exception object live for a long time, because they will hold onto the frames and all associated local variables are not going to be garbage collected.

This matter quite a lot.

For example it is one reason why you need to call the close method on file objects even if you don’t create reference cycles, because the file object may be referenced by a traceback object stored on an Exception. This exclude file from being garbage collected even after it goes out of scope.

The issue is worse in interactive python shells like (Jupyter) where each unhandled exceptions ends up leaving forever in few places. I’m working on a way to clear that up hence I’ve found this issue.

In Python, the types module provides the FrameType and TracebackType types, which represent frames and tracebacks, respectively. However, you cannot instantiate these types directly. https://docs.python.org/3/library/types.html#types.FrameType

The tracback attribute was introduced in python 3 with PEP 3134, it goes a bit on ramification of this change in details, so it is a good read for curious.

Answered By: Piotr Czapla
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.