I've encountered an interesting behavior regarding the from ... import ...
syntax. It seems that when using this syntax, identifiers are imported as new identifiers, instead of references.
I've been writing Python for quite some time and have always avoided using this syntax, so this is quite unexpected for me. Instead I expected it to work the same as import ...
.
Here is an example:
a.py
x = 0
b.py
from b import x
def f():
print(x)
main.py
import a
import b
a.f()
b.x = 1
a.f()
The result is:
0
0
instead of
0
1
which I expected.
So does from ... import ...
create a new variable, or does it make a reference to the variable?
I've searched the docs, Googled, searched around StackOverflow, but there doesn't seem to be a very clear answer to this question. Any links (especially Python docs) would be appreciated.