I was playing with scopes and namespaces and I found a weird behaviour which I'm not sure how to explain. Say we have a file called new_script.py with inside
a = 0
def func():
import new_script #import itself
new_script.a += 1
print(new_script.a)
func()
print(a)
when executing it prints
1
1
2
0
I didn't expect the last print of the number 0
. From what I understand, it prints the first two 1
executing the self-import statement incrementing the global a
, then it prints 2
because it increments again the global a
from inside the function, but then why the last print is 0
instead of 2
?