Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how do I obtain the current frame?

Tags:

python

In other languages I can obtain the current frame via a reflection api to determine what variables are local to the scope that I an currently in.

Is there a way to do this in Python?

like image 860
chollida Avatar asked Jul 16 '09 20:07

chollida


People also ask

What is inspect module in Python?

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects.

What is frame object in Python?

In Python, when you call a function you create something called a stack frame. This frame is (as you see in your example) basically just a table of all of the variables that are local to your function.


1 Answers

import sys     sys._getframe(number) 

The number being 0 for the current frame and 1 for the frame up and so on up.

The best introduction I have found to frames in python is here

However, look at the inspect module as it does most common things you want to do with frames.

like image 123
David Raznick Avatar answered Sep 19 '22 21:09

David Raznick