In order to bind a name to a variable from an outer-scope, we can use global
or nonlocal
keyword, but with the variable name known beforehand, e.g.,
x0 = 0
def f1():
x1 = 1
def f2():
x2 = 2
def f3():
x3 = 3
def h():
global x0 # cannot use nonlocal, in case you're curious like me
nonlocal x1, x2, x3
x0 += x0
x1 += x1
x2 += x2
x3 += x3
print(f'x0: {x0}, x1: {x1}, x2: {x2}, x3: {x3}')
return h
return f3()
return f2()
call_h = f1()
call_h() # print on screen: x0: 0, x1: 2, x2: 4, x3: 6
To dynamically bind a name to a global variable, we can use globals()
dictionary instead (globals()['x0']
). Is there a way to do this with nonlocal variables? Is there nonlocals()['x1']
sort of thing?
Edit 1: Clarification A suggested duplicate question did not require modifications of the nonlocals, whereas mine does.