I have the following code
data = 10
def func():
print data
data += 1
print data
Here, the code fails with the following error
UnboundLocalError: local variable 'data' referenced before assignment
i have read this post and understood that my code is creating a local variable named data and so, its not trying to look in the global scope. But, i am trying to print data before the assignment. So, why isn't data accessible before the assignment?
NOTE: I know that if i use global keyword, variable will be accessible. My question is why it isn't accessible before the assignment operation. Because, only when assigning, it creates the variable in local scope.