0

I am searching for a method to sharing a global variable across multiple files. I found this and the second answer brought me here. So now I know how to do it. For example:

# config.py
THIS_IS_A_GLOBAL_VARIABLE = 1
# sub.py
import config

def print_var():
    print('global variable: ', config.THIS_IS_A_GLOBAL_VARIABLE)
# main.py
import config
from sub import print_var

print_var()   # global variable:  1

config.THIS_IS_A_GLOBAL_VARIABLE = 2
print_var()   # global variable:  2

This is exactly what I want.

The question is, I am curious that why it works? Here is a simple explanation: Because there is only one instance of each module, any changes made to the module object get reflected everywhere. But I still don't fully understand it. Is there any further explanation about it?
Very thanks!

Chun-Ye Lu
  • 343
  • 1
  • 2
  • 10
  • 2
    As a programmer you should try very, *very*, **very** hard not to use globals. – quamrana Aug 05 '21 at 19:43
  • Mandatory link to [Ned Batchelder](https://nedbatchelder.com/text/names.html) – quamrana Aug 05 '21 at 19:43
  • 3
    It works because there is only one `config` module when your program runs. A module is an object, just like other objects. So, both `sub.py` and `main.py` are importing the same module object. When you change a member in that object, everyone with a reference to that object will see it. main.py imports it first. When sub.py imports it, it's already in the global module list, so it just takes a second reference to that module object. – Tim Roberts Aug 05 '21 at 19:45
  • @quamrana yes I know it, I use the global variable for the convenience. Without the global variable it would be much complex. And I only use one global variable. Thanks for the reminder! – Chun-Ye Lu Aug 05 '21 at 20:05
  • @TimRoberts Thanks for your explanation! But I have another question. Why it still works if I change the import order? That is, if I import `sub` first and then import `config`, it still works. – Chun-Ye Lu Aug 05 '21 at 20:09
  • 1
    There's still only one `config` module object. The order doesn't matter. The first one in causes the file to be read and interpreted. The second one just get a reference to the existing object. – Tim Roberts Aug 05 '21 at 20:41

1 Answers1

1

The bit that works is here: config.THIS_IS_A_GLOBAL_VARIABLE = 2

What is happening is that config is a reference to the module config.py. Then THIS_IS_A_GLOBAL_VARIABLE is one of the attributes in that module and the above assignment make the attribute refer to a different object.

Now, any other module which has the line: import config refers to the same module and when you fetch any attributes you get hold of the same references.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • If I modify `main.py`: `from config import THIS_IS_A_GLOBAL_VARIABLE`, it would make a copy of `THIS_IS_A_GLOBAL_VARIABLE` in `main.py`. And if I change the value of `THIS_IS_A_GLOBAL_VARIABLE` in `main.py`, the result of `print_var` would not be affected because I just modify a local variable in `main.py`. Did I understand it correctly? – Chun-Ye Lu Aug 05 '21 at 20:01