I want to modify a global variable in different python files, and I read about this thread: Using global variables between files?
In the above example, the global variable is a list reference so the modification of the list elements can be correctly done. However, if the global variable is, for example, a string, then it doesn't work.
In the below code, I try to modify the global variable defined in setting.py
.
## setting.py
s = 'setting'
## main.py
from setting import s
import dau1
import dau2
if __name__ == "__main__":
print(s)
print('After mother')
s = 'mother'
print(s)
dau1.dau1call()
print(s)
dau2.dau2call()
print(s)
## dau1.py
from setting import s
def dau1call():
global s
print('After dau1')
s = 'dau1'
## dau2.py
from setting import s
def dau2call():
global s
print('After dau2')
s = 'dau2'
The result is
setting
After mother
mother
After dau1
mother
After dau2
mother
Is there any way to modify a global variable in different .py files?