I have a dictionary that contains several items. Inside a method, I want to make these available. This is what I am doing right now
def myFunc(stuff):
for foo, bar in stuff.items():
locals()[foo] = bar
testVar
foo = {'testVar': 0}
myFunc(foo)
However, I get (inside python 2.7)
NameError: global name 'testVar' is not defined
Perhaps doing this, I am not accessing the function scope but the local scope outside of it? How can I access the function scope?
Emphasis
This is an abstracted problem to show exactly what I want. In this scenario, ofc., accessing a dictionary would be the correct solution. But let's just assume that I have some reasons for doing this. The question is not:
- How can I access variables I stored in a dictionary? Should I use the dictionary?
It is
- Given that I operate inside a function, and want to access the variables directly in that function, how do I have to unzip the dictionary?