In bar.py:
var = 1
def set_var():
global var
var = 2
In foo.py:
from bar import *
print(var)
set_var()
print(var)
In foo2.py:
import bar
print(bar.var)
bar.set_var()
print(bar.var)
If I run foo.py the output is:
1
1
but if I run foo2.py the output is:
1
2
which is what I would expect.
I only would like to understand this behavior, since I am new to Python and haven't find a good reason for this.
PD: Additional info. I want to develop a module that uses a singleton object and I have some legacy code that uses this object. I would prefer not to prefix every reference to that object in the legacy code, so that is why I though that importing the module with the from library import object syntax would help.
So in the library I have functions that access the global object to configure it just as in the example (bar.py). And in the legacy code I hoped it only would be needed to do some kind of import as it is done in foo.py.
Thanks for the guidance.
EDITED: SECOND EXAMPLE
In bar.py
var_list = list(range(0, 2))
var_list2 = list(range(0, 2))
def set_var():
global var_list
var_list = list(range(0, 3))
var_list2.append(2)
In foo.py
from bar import *
print(var_list)
print(var_list2)
set_var()
print(var_list)
print(var_list2)
The output is:
[0, 1]
[0, 1]
[0, 1]
[0, 1, 2]
I understand that in set_var we are creating a new object with list(), but I would expect in foo.py to access this new object when I refer to it with var_list (just as it would work using the syntax bar.var_list). I need some more background.
Thanks