This question stems from curiosity rather than a need to solve some particular problem. Given the two snippets of code below, why does the first produce an unbounded local error while the second does not? They appear to have the same critical sequence of operations in my mind, but there must be some under-the-hood magic at work that I am missing.
x=5
def fun1():
x=x+2 # or x+=2
print x
def fun2():
z=x+2
print z
I understand that to make the first function work, the global function must be passed. I just want to know what the interpreter does to make those two statements much more different than they seem. Thanks.
- This question is different from similar questions in that it asks for the underlying cause of this behavior and not a solution for mitigating it.