This code works as expected:
def my_fun():
a = b
a = 5
print a
b = 2
my_fun()
print b
And I get:
5
2
But if I do:
def my_fun():
a = b
a = 5
b = 1
print a
b = 2
my_fun()
print b
I do get the error: UnboundLocalError: local variable 'b' referenced before assignment
What happens here ? Although b
is visible to the function, I cannot change it inside the function ?