Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of Globals
#1
Looking for some clarification on the use of global variables.

Suppose I need a single variable called "Logged_In" that's available everywhere in my program (eg in multiple functions).

Can I simply declare it as global in the main code or must I also declare it as global in all the functions that need to use it?

Thanks
Reply
#2
It is not recommended to use globals if you can avoid this. If you want a function to use a variable, pass it as a parameter. There is another way - make a class

class MyClass:
    num = 10
Then:

def func():
    print(MyClass.num)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Thanks. Understood it's not good practice to use global variables but just trying to get my head around how python expect them to work.
Reply
#4
Well, they work like this:

va1 = 1
var2 = 2

def func():
    var1 = 3
    var2 = 4
    print(var1, var2)

func () # output: 3 4
In order to print 1 2 you have to declare that you want to use the global variables.

def func_global():
    global var1
    global var2
    print(var1, var2)

func_global() # output: 1 2
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Nov-06-2017, 01:11 PM)CWatters Wrote: Understood it's not good practice to use global variables but just trying to get my head around how python expect them to work.

Hope this example helps
var1 = 1
var2 = 2
var3 = 3
var4 = 4

def foo():
    global var1
    print('var1 value within foo, before addition is {}'.format(var1))
    var1 += 10
    print('var1 value within foo, after addition is {}'.format(var1))
    
def bar():
    # variable with same name, but local namespace
    var3 = 0 # this is actually local var, there is also 'global' one with different value
    print('var3 value within bar, before addition is {}'.format(var3))
    var3 += 1
    print('var3 value within bar, after addition is {}'.format(var3))
    
def foo2():
    print('var4 value from within foo2 is {}'.format(var4))

def bar2():
    var2 += 10 # try to change value of var2 this will rise error
    print('var2 value within bar, before addition is {}'.format(var2))
       

    
print('var1 value before call of the foo/bar is {}'.format(var1))
print('var2 value before call of the foo/bar is {}'.format(var2))
print('var3 value before call of the foo/bar is {}\n\n'.format(var3))
foo()
print('var1 value after call of the foo is {}\n\n'.format(var1))
bar()
print('var3 value after call of the foo is {}\n\n'.format(var3)) # this refer to the 'global' one, value is still 3.
print('var4 value before call of the foo2 is {}'.format(var4))
foo2()
print('var4 value after call of the foo2 is {}\n\n'.format(var4))
bar2()
Output:
var1 value before call of the foo/bar is 1 var2 value before call of the foo/bar is 2 var3 value before call of the foo/bar is 3 var1 value within foo, before addition is 1 var1 value within foo, after addition is 11 var1 value after call of the foo is 11 var3 value within bar, before addition is 0 var3 value within bar, after addition is 1 var3 value after call of the foo is 3 var4 value before call of the foo2 is 4 var4 value from within foo2 is 4 var4 value after call of the foo2 is 4 Traceback (most recent call last): File "C:\myfile.py", line 38, in <module> bar2() File "C:\myfile.py", line 23, in bar2 var2 += 10 # try to change value of var2 this will rise error UnboundLocalError: local variable 'var2' referenced before assignment >>>
Reply
#6
Sorry I forgot to say thank you for the replies. They make thinks a lot clearer.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  why globals() exits python when quit() is aliased as q abc12346 4 687 May-23-2023, 08:56 AM
Last Post: Gribouillis
  How to avoid exec(), globals(), locals(), eval() paul18fr 10 5,018 Apr-21-2021, 05:53 PM
Last Post: snippsat
  accessing globals from a function Skaperen 8 4,136 Sep-01-2018, 11:50 PM
Last Post: Skaperen
  Globals vs Function mcmxl22 2 3,340 Feb-04-2017, 11:38 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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