I want to create variables inside a function from a dictionary.
Let’s say I have a dictionary, bar:
bar = { 'a': 1, 'b': 2, 'c': 3 }
That goes to:
def foo(): a = 1 b = 2 c = 3
I want to make new variables with the variable names as bar‘s keys (a, b, c), then set the values of the variables to the value of the corresponding key.
So, in the end, it should be similar to:
bar = { k: v } # ---> def foo(): k = v
Is it possible? And if so, how?
Your question is not clear.
If you want to ‘set’ said variables when foo is not running, no, you can’t. There is no frame object yet to ‘set’ the local variables in.
If you want to do that in the function body, you shouldn’t (check the python documentation for locals()).
However, you could do a
foo.__dict__.update(bar), and then you could access those variables even from inside the function as foo.a, foo.b and foo.c. The question is: why do you want to do that, and why isn’t a class more suitable for your purposes?