Consider the following code:
def g():
a = {}
b = 0
def f():
a[0] = b
f()
print(a)
return a
a = g()
print(a)
It gives the following output when executed:
{0: 0}
{0: 0}
But if I try to update b
in f()
as in
def g():
a = {}
b = 0
def f():
a[0] = b
b+=1
f()
print(a)
return a
a = g()
print(a)
It throws the following error:
UnboundLocalError: local variable 'b' referenced before assignment
Is this expected? I need to update b
inside f()
. Is that impossible?