Jan-03-2019, 03:39 AM
If you want to share variables between modules, it is desirable to read official recommendations first.
Nevertheless, if you would use mutable data types, e.g. dict, you could get the code worked as expected.
one.py:
Nevertheless, if you would use mutable data types, e.g. dict, you could get the code worked as expected.
one.py:
dct = dict() dct['i1'] = 1 dct['s1'] = "1.1.1.1" print ("file_1.py: i1 = " + str(dct['i1']) + ", s1 = " + str(dct['s1'])) def Initialization(): # global i1 # global s1 dct['i1'] = 10 dct['s1'] = "10.10.10.10" print ("file_1.py Initialization(): i1 = " + str(dct['i1']) + ", s1 = " + str(dct['s1'])) return()two.py:
from one import * print ("file_2.py: i1 = " + str(dct['i1']) + ", s1 = " + str(dct['s1'])) def show_vars() : # global i1 # global s1 print ("file_2.py show_vars(): i1 = " + str(dct['i1']) + ", s1 = " + str(dct['s1'])) return ()three.py
from one import * from two import * print ("file_3.py STEP 1: i1 = " + str(dct['i1']) + ", s1 = " + str(dct['s1'])) print ("file_3.py STEP 2: We are going to initialize the global vars.") Initialization() print ("file_3.py STEP 3: i1 = " + str(dct['i1']) + ", s1 = " + str(dct['s1'])) print ("file_3.py STEP 4: Call show_vars() to print the global vars value.") show_vars()... run three.py ...
Output:file_1.py: i1 = 1, s1 = 1.1.1.1
file_2.py: i1 = 1, s1 = 1.1.1.1
file_3.py STEP 1: i1 = 1, s1 = 1.1.1.1
file_3.py STEP 2: We are going to initialize the global vars.
file_1.py Initialization(): i1 = 10, s1 = 10.10.10.10
file_3.py STEP 3: i1 = 10, s1 = 10.10.10.10
file_3.py STEP 4: Call show_vars() to print the global vars value.
file_2.py show_vars(): i1 = 10, s1 = 10.10.10.10