[edit] I renamed testons_constants to testons_variables to make more clear that I want to be able to modify the variables. If in the comment testons_constants appear take in account that it corresponds to the new name testons_variables
I am trying to understand how the variables are shared between files in python.
I made for this purpose three files:
testons.py:
import testons_variables
import testons_functions
# The goal of this file is to understand how python deals with variables shared between files
print(testons_variables.A) # We can access the variable from the other file
testons_variables.A=5 # We can modify its value
print(testons_variables.A)
testons_functions.fct()
print(testons_variables.A) # The value has been modified from testons_functions
testons_variables.py:
A=0
testons_functions.py
import testons_variables
def fct():
print(testons_variables.A) # this print will show the value of the variable from testons before the call of the function, and not from
# testons_variables
testons_variables.A=50 # We can modify the value
print(testons_variables.A)
Here are the outputs when I run testons.py:
0
5
5
50
50
Now, doing "reverse understanding", I realize that wherever the variable testons_variables.A is being modified, it will be modified for all files using it. Indeed, if I modify it in the file testons.py, it will be also modified inside testons_functions.py. And if I modify it in testons_functions.py, it will also be modified for testons.py.
This is my understanding now. So it seems that variables shared between files are modified for everyone, wherever the modification has been done.
What confuses me is that if we use global variable, to modify them within a function we need the keyword "global" to allow the global modification. I never used such keyword here. I know it is not exactly the same situation (I am not using a global variable inside function within a unique file), but I am anyway disturbed by this behavior which makes me think that I am probably missing a point here.
I am then wondering if I understood properly what happens when variable are being shared between files, and this is my question.