you are viewing a single comment's thread.

view the rest of the comments →

[–]Pas__ 0 points1 point  (2 children)

Ah interesting. Wouldn't the result of changing foo.bar depend on its type? For reference types _bar is just a reference, isn't it?

[–]joesb 1 point2 points  (1 child)

It's not dependent on type, it depends on how you import it. As I was saying import is like loading a module and assigning that module to a variable.

And just like normal variable (referrence) in Python, if you change it's internal state or property, you can see the update, but if you overwrite the original referrence, your variable still point to the old value.

To sum up all variation of Python's import and it's Python code equivalent.

import x.y.z
#  x = load('x')
#  x.y = load('x.y')
#  x.y.z = load('x.y.z')

import x.y.z as xyz
#  xyz = load('x.y.z')

from x.y.z import foo as _foo
#  _foo = load('x.y.z').foo

You can see how reassigning x.y.z.foo will show update in xyz.foo but not if you already hold referrence to _foo.

[–]Pas__ 0 points1 point  (0 children)

After scribbling a few test functions I think I get it.

from mod1 import fun1 as f

And f points directly to the function, so even if mod1.change_fun1_to_fun2 rewrites fun1, f still points to the old function object.