I'm using Python2.7 and still quite confused about scoping in python. I can't explain why the situation can happen. Somebody could help me. Thanks in advance.
case 1:
x = 1
def func():
print x
func()
=> result:
1
case 2:
x = 1
def func():
print x
x = 9
func()
=> result:
UnboundLocalError: local variable 'x' referenced before assignment
When I add the line x = 9
in case 2, an error occurred.