Again, when you run the code from the command line as
python test1.py
The code is compiled and ran in a module which name is not 'test1' but '__main__' because it is the main script. Then when the code calls
change()
, this function runs
import test1
. This causes the file to be read again because the filename is test1.py, the code is ran again in a new module which name is 'test1'. So there are now two modules with the same code and there are two 'x' variables.
The 'import test1' statement triggers a call to the new function change() from module test1. This call sets test1.x to the value 2 and prints x. Then the 'import test1' is completed and the first call to
__main__.change()
can complete, but it doesn't change
__main__.x
which value is still 1, so the last print statement prints 1.
Here is a modified code to show the different steps
#test1.py
x=1
def change():
print("Entering change() in module", __name__)
import test1
test1.x=2
print("Exiting change() in module", __name__)
change()
print("Printing x in module", __name__)
print(x)
Output:
λ python3 test1.py
Entering change() in module __main__
Entering change() in module test1
Exiting change() in module test1
Printing x in module test1
2
Exiting change() in module __main__
Printing x in module __main__
1