Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
use of global
#1
I need to use pass the values in some variables from the "main" script to functions, and also from one function to another function. That seems to work OK when the functions and the main script are in one module. But if I move the functions into a separate module and import that module into the main script, then I get weird error messages (NameError, name is not defined" messages)-type messages. Reading and rereading the python manuals does not provide a solution.

I did read on this forum that many of you recommend NOT to use global variables. I wonder why that is. Is that because using global variables in the correct way is difficult; or is it because some of you have experienced that there are bugs in python that cause that de use of global variables does not always work as decoumented in the manuals? I can, if neccessary, include here some simple "test" programs that show the difficulty.
Reply
#2
https://stackoverflow.com/questions/1915...ables-evil
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thx for the very quick reply. I had read that information already before, and I think that my "test" scripts are in line with what is recommended there. Still, it does not function as intended.
I have attached here my two scripts (the main script that is ran, and the imported script with the functions:
# this is file "testglobalsextmain.py"

from testglobalsextimport import *

global a,b,c

a = "a"
b = "b"
c = "c"
x = "?"

print ("main ",a,b,c,x)
    
d=f1(a)
d=f2(a)
b = "b2"
d=f2(a)

print ("end  ",a,b,c,x)

#and

# this is file "testglobalsextimport.py"

def f1(x):
    global a,b,c
    b = "f1b"
    c = "f1c"
    print ("f1   ",a,b,c,x)
    
def f2(x):
    global a,b,c
    print ("f2   ",a,b,c,x)
When I run the "main" script, I get this output:
Output:
>>> RESTART: G:\UserData\Willy\Documents\Willys_Programmas\Test_routines\testglobalsextmain.py main a b c ? Traceback (most recent call last): File "G:\UserData\Willy\Documents\Willys_Programmas\Test_routines\testglobalsextmain.py", line 14, in <module> d=f1(a) File "G:\UserData\Willy\Documents\Willys_Programmas\Test_routines\testglobalsextimport.py", line 7, in f1 print ("f1 ",a,b,c,x) NameError: name 'a' is not defined >>> Can anybody please tell me what I did wrong?
Reply
#4
Hi!

I'm just a newbie, but I was a bit shocked by your cumbersome code. Maybe it's just a test for a longer and more difficult program, otherwise I wouldn't understand the point of it. But I see that in your file "testglobalsextmain.py":

# this is file "testglobalsextmain.py"

from testglobalsextimport import *

global a,b,c

a = "a"
b = "b"
c = "c"
x = "?"
you are calling this file "testglobalsextimport.py":

# this is file "testglobalsextimport.py"

def f1(x):
    global a,b,c
    b = "f1b"
    c = "f1c"
    print ("f1   ",a,b,c,x)
    
def f2(x):
    global a,b,c
    print ("f2   ",a,b,c,x)
Where I can see that you haven't defined 'a', so maybe that's why you are getting this output:

Error:
NameError: name 'a' is not defined
I don't know, but if you want just to print strings, why all the bother defining functions? Wouldn't be so much simpler just writing commands to directly print what you want?

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#5
Actually it's a perfect example as to why using global variables should be avoided... There are problems on so many levels. You need to read about scope and namespaces.

In your testglobalsextimport.py a is not defined whatsoever. b and c are defined as global variables, BUT only if you call f1(). If you don't call f1 and call just f2 they will not be defined too. functions in testglobalsextimport.py cannot know about variables in the module where they just may be imported...

Then just for experiment, let's change your and remove a altogether:

def f1(x):
    global b,c
    b = "f1b"
    c = "f1c"
    print ("f1   ",b,c,x)
     
def f2(x):
    global b,c
    print ("f2   ",b,c,x)
now, if you run testglobalsextmain.py you will get

Output:
main a b c ? f1 f1b f1c a f2 f1b f1c a f2 f1b f1c a end a b2 c ?
as you can see, changing b value to b2 has no effect on b variable in testglobalsextimport.py
Now just imagine what kind of nightmare you would have to debug such code.


Few more observations:
You don't need to declare them globals on line 3. You use global statement only in the narrow scope (e.g. function f1) when you will change values of global variables. If you don't declare them global and there is no local variable with same name the module level global variable will be accessible within the function, e.g.

foo = 'spam'
def bar():
    print(foo)

bar()
in the above code it will print 'spam' - global variable foo is accessible in the function.

You call your functions and assign the returned value to d. That is not neccessary. Your functions do not return anything and you can just call them. At the moment the value of d is None because they don't return anything explicitly.

You are using star import - from testglobalsextimport import *. Like globals this is bad practice and strongly discouraged.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Sep-30-2019, 09:13 AM)wpo Wrote: Can anybody please tell me what I did wrong?

I just noticed that the error message already pointed at that:

Error:
File "G:\UserData\Willy\Documents\Willys_Programmas\Test_routines\testglobalsextimport.py", line 7, in f1 print ("f1 ",a,b,c,x) NameError: name 'a' is not defined
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#7
Hello, This code is so "useless" because I wrote it just to make my point (the program that is strungling with a similar difficulty is 4000+ lines of code, I don't think I wnat to bother you with all that!!!
In f1 I did not assing a value to "a" because I want the function to use the global variable defined in "main". But even including the required "glbal" statements in the right places does not make the "a" from "main" availble in "f1".
Not, when I copy the contents of the "import" module into the "main" function and remove the "import" statement, it functions!

See this version of the "main" function with the functions from "import" included.
# this was file "testglobalsextimport.py"

def f1(x):
    #global a,b,c
    b = "f1b"
    c = "f1c"
    print ("f1   ",a,b,c,x)
def f2(x):
    global a,b,c
    print ("f2   ",a,b,c,x)

# this was file "testglobalsextmain.py"

#from testglobalsextimport import *
global a,b,c
a = "a"
b = "b"
c = "c"
x = "?"
print ("main ",a,b,c,x)
   
d=f1(a)
d=f2(a)
b = "b2"
d=f2(a)
print ("end  ",a,b,c,x)
This module gives the following result:
Output:
>>> RESTART: G:\UserData\xxxxx\Documents\xxxxxx_Programmas\Test_routines\testglobalsint1.py main a b c ? f1 a f1b f1c a f2 a b c a f2 a b2 c a end a b2 c ? >>>
Reply
#8
(Sep-30-2019, 10:38 AM)wpo Wrote: In f1 I did not assing a value to "a" because I want the function to use the global variable defined in "main". But even including the required "glbal" statements in the right places does not make the "a" from "main" availble in "f1".
Not, when I copy the contents of the "import" module into the "main" function and remove the "import" statement, it functions!

As I said, the functions in testglobalsextimport.py could not possibly "know" about variables in other mnodules (unless you import them, but in this case you will create circular import).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Hi again!

(Sep-30-2019, 10:38 AM)wpo Wrote: Hello, This code is so "useless" because I wrote it just to make my point (the program that is strungling with a similar difficulty is 4000+ lines of code, I don't think I wnat to bother you with all that!!!

Yeah, I thought that could be the case.

(Sep-30-2019, 10:38 AM)wpo Wrote: In f1 I did not assing a value to "a" because I want the function to use the global variable defined in "main". But even including the required "glbal" statements in the right places does not make the "a" from "main" availble in "f1".
Not, when I copy the contents of the "import" module into the "main" function and remove the "import" statement, it functions!

Yes, because now, 'a' is defined in the running program. It doesn't call for another file, where 'a' was not defined.

All the best,



I was just thinking that maybe, you could give some value to 'a' in the file to be called, (I think it could be an empty string like a =""), and then, reassign a new value, the value you really want, to 'a'. It's just a thought ...

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#10
I've been experimenting further and I think that I got most of it working! See below. "most of it" because I conclude that passing values (in global variables) between functions that reside in different modules is (almost???) impossible. Between modules, values are always to be passed as arguments in function calls and in returns.
But it is possible to "expose" variables from one function to another function, as long as both the "source" function and the "target" function are coded within the same module. If a function needs to modify the content of a variable that was defined in another function, then the "modifying" function needs to contain a "global" statement for that variable. That way the value of a "global" variable can be modified by any other function in that same module.

If I still am missing something, can someone tell me please?

example:
# this is file "testglobalsextmain2.py"

global a,b,c
x="x"
a = "am"
b = "bm"
c = "cm"


from testglobalsextimport2 import *
d=finit(x)
print ("main ",a,b,c,x)
   
d=f1(x)
d=f2(x)
d=f3(x)
d=f5(x)
b = "b2"
d=f2(x)

print ("end  ",a,b,c,x)
# this is file "testglobalsextimport2.py"

def finit(x):
    global a,b,c
    a = "ai"
    b = "bi"
    c = "ci"

def f1(x):
    global a,b,c
    b = "f1b"
    c = "f1c"
    print ("f1   ",a,b,c,x)
    
def f2(x):
    #global a,b,c  does not modify any global!
    print ("f2   ",a,b,c,x)

def f3(x):
    #global a,b,c  does not modify any global!
    print ("f3   ",a,b,c,x)
    y = f4(x)

def f4(x):
    global a,b,c
    b = "f4b"
    c = "f4c"
    print ("f4   ",a,b,c,x)
    b = "f4ba"
    c = "f4ca"
    y = f2(x)

def f5(x):
    global a,b,c
    b = "f5ba"
    c = "f5ca"
    print ("f5   ",a,b,c,x)
and the output is:

Output:
RESTART: G:/UserData/xxxxx/Documents/xxxxxxx_Programmas/Test_routines/testglobalsextmain2.py main am bm cm x f1 ai f1b f1c x f2 ai f1b f1c x f3 ai f1b f1c x f4 ai f4b f4c x f2 ai f4ba f4ca x f5 ai f5ba f5ca x f2 ai f5ba f5ca x end am b2 cm x
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Global variable does not seem to be global. Columbo 6 3,671 Jul-15-2019, 11:00 PM
Last Post: Columbo

Forum Jump:

User Panel Messages

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