So i want to get every variable from a function as a dictionary. Like this:
from typing import Any
def myfunc():
var1 = 10
var2 = 20
var3 = 30
var4 = '40'
var5 = False
x: dict[str, Any] = myfunc.vars
hardtyped_x = { # What i want x to be. Not including parameters
'var1': 10,
'var2': 20,
'var3': 30,
'var4': '40',
'var5': False
}
I tried asking tabnine and got something along the lines of:
import inspect
def myfunc():
var1 = 10
var2 = 20
var3 = 30
var4 = '40'
var5 = False
x = inspect.currentframe().f_back.f_locals
but this either raises an error (as currentframe() can return None and this raises an error) or i get a long list of dunder methods and other things of the sort
i figured out a psuedo way to do it:
Will update once im done making the full function
Edit: JK found a different way to do what i wanted
Edit2: JK again i made it anyway for a different project