You can use a global variable within other functions by declaring it as global within each function that assigns a value to it:
globvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print(globvar) # No need for global declaration to read value of globvar
set_globvar_to_one()
print_globvar() # Prints 1
Since it's unclear whether globvar = 1 is creating a local variable or changing a global variable, Python defaults to creating a local variable, and makes you explicitly choose the other behavior with the global keyword.
See other answers if you want to share a global variable across modules.
Answer from Paul Stephenson on Stack OverflowYou can use a global variable within other functions by declaring it as global within each function that assigns a value to it:
globvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print(globvar) # No need for global declaration to read value of globvar
set_globvar_to_one()
print_globvar() # Prints 1
Since it's unclear whether globvar = 1 is creating a local variable or changing a global variable, Python defaults to creating a local variable, and makes you explicitly choose the other behavior with the global keyword.
See other answers if you want to share a global variable across modules.
If I'm understanding your situation correctly, what you're seeing is the result of how Python handles local (function) and global (module) namespaces.
Say you've got a module like this:
# sample.py
_my_global = 5
def func1():
_my_global = 42
def func2():
print _my_global
func1()
func2()
You might be expecting this to print 42, but instead, it prints 5. As has already been mentioned, if you add a 'global' declaration to func1(), then func2() will print 42.
def func1():
global _my_global
_my_global = 42
What's going on here is that Python assumes that any name that is assigned to, anywhere within a function, is local to that function unless explicitly told otherwise. If it is only reading from a name, and the name doesn't exist locally, it will try to look up the name in any containing scopes (e.g. the module's global scope).
When you assign 42 to the name _my_global, therefore, Python creates a local variable that shadows the global variable of the same name. That local goes out of scope and is garbage-collected when func1() returns; meanwhile, func2() can never see anything other than the (unmodified) global name. Note that this namespace decision happens at compile time, not at runtime -- if you were to read the value of _my_global inside func1() before you assign to it, you'd get an UnboundLocalError, because Python has already decided that it must be a local variable but it has not had any value associated with it yet. But by using the 'global' statement, you tell Python that it should look elsewhere for the name instead of assigning to it locally.
(I believe that this behavior originated largely through optimization of local namespaces -- without this behavior, Python's VM would need to perform at least three name lookups each time a new name is assigned to inside a function (to ensure that the name didn't already exist at module/builtin level), which would significantly slow down a very common operation.)
Is there a way to make a variable global to all functions?
How to make variables global by default, Python 3.11
Why not use global variables?
Global variables shared across modules
Videos
Fairly new to Python, is there a way to make a variable global to all functions (so I can edit them) instead of having to use the global keyword in every single function.
I have people telling me to avoid using global variables in my functions. But why should I?
In the first case the global keyword is pointless, so that is not correct. Defining a variable on the module level makes it a global variable, you don't need the global keyword.
The second example is correct usage.
However, the most common usage for global variables are without using the global keyword anywhere. The global keyword is needed only if you want to reassign the global variables in the function/method.
You need to use the global keyword in a function if you use the global variable in a way that would otherwise be interpreted as an assignment to a local variable. Without the global keyword, you will create a local variable that hides the global in the scope of the function.
Here are a few examples:
global_var = 1
def example1():
# global keyword is not needed, local_var will be set to 1.
local_var = global_var
def example2():
# global keyword is needed, if you want to set global_var,
# otherwise you will create a local variable.
global_var = 2
def example3():
# Without using the global keyword, this is an error.
# It's an attempt to reference a local variable that has not been declared.
global_var += 1