Consider we have these Python files:
# config.py
a = 1
And:
# m.py
import config
config.a = 2
And finally:
# main.py
import config
import m
print(config.a) # -> prints 2
print(config.a)
prints 2.
But then if we change the importation method in m.py
like so:
# m.py
from config import a
a = 2
Running main.py
again prints 1 instead of 2; Python treats it now as if it's a copy of the original config.a
variable instead of a reference.
Either a
and config.a
are the same object. If so, why does its value change when we reference it as config.a
but nothing happens if we reference it as just a
?
This answer from Micheal addressed this same issue but it still didn't explain why this happens; we still don't understand why Python is treating each reference differently.