Python Forum
Use of global variables from several modules.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of global variables from several modules.
#1
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()
Reply
#2
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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/pyt...ctions.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 
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,065 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 985 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,102 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 905 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Global variables not working hobbyist 9 4,619 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,597 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,128 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Question regarding local and global variables donmerch 12 4,978 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,359 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  Where to put the global keyword when assigning variables outside a function? new_to_python 8 2,886 Feb-09-2020, 02:05 PM
Last Post: new_to_python

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020