I use vars() function for the first time, and noticed this behaviour:
nodes = ['one', 'two', 'three']
for node in nodes:
vars()[node + '_'] = 'some calc ' + node
vars()[node] = vars()[node + '_']
print one
With this snippet Python outputs some calc one
as expected, but if I use it inside function like this:
def main():
nodes = ['one', 'two', 'three']
for node in nodes:
vars()[node + '_'] = 'some calc ' + node
vars()[node] = vars()[node + '_']
print one
main()
it outputs NameError: global name 'one' is not defined
vars() object is dict:
{'node': 'three', 'three_': 'some calc three', 'two': 'some calc two', 'one': 'some calc one', 'two_': 'some calc two', 'three': 'some calc three', 'nodes': ['one', 'two', 'three'], 'one_': 'some calc one'}
Now, I would like to know what's going on as this function is not well documented and I can't find example how to return those variables if inside function