I want to define a function inside a loop using and changing variables which have been defined within the same loop iteration.
I would expect that nonlocal
is just what i need here, but if i implement it like this
for i in range(5):
obj = {"start": i}
def update():
nonlocal obj
obj["next"] = obj["start"] + 1
print(obj)
update()
i get
File "/path/to/test.py", line 4
nonlocal obj
^^^^^^^^^^^^
SyntaxError: no binding for nonlocal 'obj' found
using Python 3.10.12 on Linux.