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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With