Python Forum
Use of global variables from several modules. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Use of global variables from several modules. (/thread-15086.html)



Use of global variables from several modules. - Jstechg - Jan-02-2019

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 :
-----------


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()



RE: Use of global variables from several modules. - ichabod801 - Jan-03-2019

(Jan-02-2019, 11:09 PM)Jstechg Wrote: What am I doing wrong?

You are using global variables, star imports, and not returning values from functions.


RE: Use of global variables from several modules. - woooee - Jan-03-2019

It does not matter if the function you call is in the same program or a different file. They all operate like the following code See http://www.tutorialspoint.com/python/python_functions.htm


    def initialization(i1, s1):
        ##global i1
        ##global s1
 
        i1 = 10
        s1 = "10.10.10.10"
 
        print ("file_1.py  Initialization(): i1 = " + str(i1) + ", s1 = " + str(s1))
 
        return i1, s1 



RE: Use of global variables from several modules. - scidam - Jan-03-2019

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:
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