How does Python decide which vars to enclose? You can see when a variable is defined as a name, it is happily closed, but when defined using locals()
, it is not closed.
Works great:
def outer():
x = 5
print(x)
def inner():
return x
return inner()
print(outer())
Doesn't work:
def outer():
locals()['x'] = 5
print(locals()['x'])
def inner():
return locals()['x']
return inner()
print(outer())