def foo():
m = 3
def bar():
print(m) # code 1
m=4 # code 2
bar()
foo()
UnboundLocalError: local variable 'm' referenced before assignment
Why do I get the UnboundLocalError
? I know bar
can't change the value of m
, but shouldn't bar
just be able to get the value of m
?
And when I try the code 1/code 2
separately, it all works.