button on_click yields UnboundLocalError if variable from parent scope is updated in the function
Update: dict variables do not produce the same error
Reproduce
import ipywidgets as widgets
x = 0
y = {"x": 0}
b = widgets.Button(description="Do it")
def doit(obj):
print(x, y)
# x += 1 # uncommenting makes the above print fail
y["x"] += 1 # this is ok
b.on_click(doit)
display(b)
This is not an issue of ipywidgets, but a general issue with the Python language's scope handling. A fix would be to add nonlocal x to your handler. Details are described in this stackoverflow post.
Description
button on_click yields UnboundLocalError if variable from parent scope is updated in the function
Update: dict variables do not produce the same error
Reproduce
Expected behavior
No error
Context
jupyter troubleshoot
(on colab)