CKTIS / Python
locals()
Returns a mapping describing local name bindings in the current scope.
Version: Python 3.14; repository image, production runtime not attested
Syntax
locals()Returns a mapping describing local name bindings in the current scope.
Inside an optimized function in Python 3.14 each call returns a snapshot. Changing the mapping does not rebind the local variable.
In Python 3.14 an optimized function scope returns a fresh dictionary of current local bindings on each call. Writing count=9 into the snapshot does not rebind the actual count, so both printed values are 4. At module scope locals() instead returns the very same dictionary as globals().
def check():
v = 4
first = locals()
first["v"] = 9
second = locals()
assert first is not second and v == second["v"] == 4
check()
Examples
def sample():
count = 4
snapshot = locals()
snapshot["count"] = 9
print(count, locals()["count"])
sample()
Result
4 4
In Python 3.14 an optimized function scope returns a fresh dictionary of current local bindings on each call. Writing count=9 into the snapshot does not rebind the actual count, so both printed values are 4. At module scope locals() instead returns the very same dictionary as globals().