When I execute the following code in a python shell,
def foo():
bar = 0
vars()['bar'] = 1
print('within function: ' + str(bar))
foo()
baz = 0
vars()['baz'] = 1
print('inline: ' + str(baz))
I get
within function: 0
inline: 1
Why do setting variables via vars()
seems to work inline but not when called from within a function?