0

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Michael Kim
  • 689
  • 5
  • 20
  • how can you use `from b import x ` in your `b.py` file – Haifeng Zhang Jun 19 '17 at 19:17
  • Why would the function in `a` access the name in `b`? – Ignacio Vazquez-Abrams Jun 19 '17 at 19:17
  • sorry guys, I've messed up `a` and `b` when posting. edited – Michael Kim Jun 19 '17 at 19:18
  • 1
    Please moderator, I don't think this is a duplicate... Please read carefully ok? – Michael Kim Jun 19 '17 at 19:24
  • (BTW, while it may not be a duplicate of the other question it's flagged with, I'm **certain** I've seen someone asking exactly this before, and that we do have a duplicate somewhere in our knowledge base; I'm looking for that, to be able to correct the flagging). – Charles Duffy Jun 19 '17 at 19:49
  • @CharlesDuffy The thing is, whoever marked this question as a duplicate wasn't reading carefully. It's so depressing I have to explain why it is ___NOT___ a duplicate... also, would you mind finding the post you mentioned? – Michael Kim Jun 19 '17 at 19:54
  • 1
    Yup, still working on it. Thing is, if I de-duplicate it now, then I can't give it a different duplicate flag later, which is why I'm holding off on removing the bad flag until I've found a correct one or given up. – Charles Duffy Jun 19 '17 at 19:54
  • The closest candidate I can recall is [this one](https://stackoverflow.com/questions/34168734/python-what-priority-does-global-have), which I remember because I answered it. It's not a great match. – user2357112 Jun 19 '17 at 19:55
  • ...okay, think I found something better. @MichaelKim, do you think this works? – Charles Duffy Jun 19 '17 at 19:57
  • @user2357112 I see your post, thanks. do you have a reference for _"identically-named new global variables"_ though? this is all i'm asking for – Michael Kim Jun 19 '17 at 19:58
  • @CharlesDuffy uhh, link? – Michael Kim Jun 19 '17 at 19:58
  • @MichaelKim, it's the new duplicate link -- replaced the old one; see the top of the post if you reload the page. – Charles Duffy Jun 19 '17 at 19:59
  • 1
    ["The `from` form ... goes through the list of identifiers, looks each one of them up in the module found in step (1), and **binds the name** in the **local namespace** to the **object** thus found."](https://docs.python.org/2/reference/simple_stmts.html#the-import-statement) – user2357112 Jun 19 '17 at 20:00
  • If this isn't a duplicate, you should use edits to prove why instead of simply stating why. – Makoto Jun 19 '17 at 20:01
  • @Makoto, ...to be fair, the old duplicate link *was* pretty obviously wrong on its face; the new one is a stronger contender. – Charles Duffy Jun 19 '17 at 20:02
  • 1
    @CharlesDuffy: I won't contest that, but this is more for future reference. Users edit questions as "not a duplicate" but it doesn't actually mean anything to a passer-by. Adding rationale would help everyone. – Makoto Jun 19 '17 at 20:02
  • @CharlesDuffy thanks. The new one _is_ a duplicate, but the answer there didn't provide the Python doc reference I'm looking for. – Michael Kim Jun 19 '17 at 20:07
  • @user2357112 thanks for the link! – Michael Kim Jun 19 '17 at 20:08
  • @user2357112, perhaps you might add an answer on the other question? :) – Charles Duffy Jun 19 '17 at 20:08
  • I'm editing the post now... – Michael Kim Jun 19 '17 at 20:08
  • Try making `x` a list of one element, `x = [0]`, and then change its value with `a.x[0] = 1` in the `main.py` script and see what happens. – martineau Jun 19 '17 at 20:18
  • @martineau thanks, but the issue here is how `from ... import ...` behaves, not how to solve the problem. using `import ...` would do it anyway – Michael Kim Jun 19 '17 at 20:21
  • @MichaelKim, ...btw, as a rule, we tend to frown on including answers in questions (which prevents those answers from being commented on, flagged, voted on, &c. independently of the relevant question). If something is flagged as a duplicate of a question that isn't adequately answered, the Right Thing is to *add a proper answer to the other question*, not edit an answer into the question. – Charles Duffy Jun 19 '17 at 20:24
  • @CharlesDuffy oh well, ok.. – Michael Kim Jun 19 '17 at 20:26
  • @CharlesDuffy looks good now, I guess? – Michael Kim Jun 19 '17 at 20:33
  • @CharlesDuffy thank you!!! you really helped me out – Michael Kim Jun 19 '17 at 20:40

0 Answers0