I am writting a python program that uses several modules.
In "file_1.py" the global variables "i1" and "s1" are declared. In addition, in this module the "Initialization" function is defined, that modifies the value of these variables.
In "file_2.py" the "show_vars" function is defined, which shows the value of "i1" and "s1".
In "file_3.py" the initial value of "i1" and "s1" is shown, "Initialization" is called, their value is displayed again and "show_vars" is called.
Attached at the end of this message the source code of these modules.
The output I expected to get when executing "python file_3.py" is as follows:
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
But the output I get is really:
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 = 1, s1 = 1.1.1.1
file_3.py STEP 4: Call show_vars() to print the global vars value.
file_2.py show_vars(): i1 = 1, s1 = 1.1.1.1
This means that the changes made by the "Initialization()" function, defined in "file_1.py", are not reflected
in the value of the global variables that are seen by "file_2.py" and "file_3.py"
What am I doing wrong? How can I get the expected result?
The source code of these modules is:
file_1.py :
-----------
file_2.py :
-----------
file_3.py :
-----------
In "file_1.py" the global variables "i1" and "s1" are declared. In addition, in this module the "Initialization" function is defined, that modifies the value of these variables.
In "file_2.py" the "show_vars" function is defined, which shows the value of "i1" and "s1".
In "file_3.py" the initial value of "i1" and "s1" is shown, "Initialization" is called, their value is displayed again and "show_vars" is called.
Attached at the end of this message the source code of these modules.
The output I expected to get when executing "python file_3.py" is as follows:
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
But the output I get is really:
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 = 1, s1 = 1.1.1.1
file_3.py STEP 4: Call show_vars() to print the global vars value.
file_2.py show_vars(): i1 = 1, s1 = 1.1.1.1
This means that the changes made by the "Initialization()" function, defined in "file_1.py", are not reflected
in the value of the global variables that are seen by "file_2.py" and "file_3.py"
What am I doing wrong? How can I get the expected result?
The source code of these modules is:
file_1.py :
-----------
i1 = 1; s1 = "1.1.1.1"; print ("file_1.py: i1 = " + str(i1) + ", s1 = " + str(s1)) def Initialization() : global i1 global s1 i1 = 10 s1 = "10.10.10.10" print ("file_1.py Initialization(): i1 = " + str(i1) + ", s1 = " + str(s1)) return()
file_2.py :
-----------
from file_1 import * print ("file_2.py: i1 = " + str(i1) + ", s1 = " + str(s1)) def show_vars() : global i1 global s1 print ("file_2.py show_vars(): i1 = " + str(i1) + ", s1 = " + str(s1)) return ()
file_3.py :
-----------
from file_1 import * from file_2 import * print ("file_3.py STEP 1: i1 = " + str(i1) + ", s1 = " + str(s1)) print ("file_3.py STEP 2: We are going to initialize the global vars.") Initialization() print ("file_3.py STEP 3: i1 = " + str(i1) + ", s1 = " + str(s1)) print ("file_3.py STEP 4: Call show_vars() to print the global vars value.") show_vars()