Global variables are used in programming to share memory between different sections of an application, this allows for a range of functionality such as keeping track of resource use between classes, storing statistics about an application and much more. In Python, they are mainly used to share values between classes and functions.
Understanding global variables in Python
In Python, a conventional global variable is only used to share a value within classes and functions. A variable- once declared as a global within a function or class can then be modified within the segment.
num = 1
def increment():
global num
num += 1
The above code would allow the variable 'num' to be used within that function, whereas if you were to omit the global statement, it would be undefined.
If you were to attempt to use this logic to declare a global variable from another class however, it would not work as it would in other languages such as Java or C++.
For example, if we had one class with a variable that needed to be accessed from within another imported class, there would be no way for us to access this variable using the global keyword:
main.py
import test
num = 1
test.increment()
test.py
def increment():
global num
num += 1 # num here would still be undefined
As you can see above, even though we try to access 'num' as a global within the increment() function after it has been given a value in main.py, it will still not be defined and will result in an error.
Sharing global variables between files/modules in Python
Even though the global keyword is conventionally only used to access variables from within the same module, there are ways to use it in order to share variables between files. For example, we could take influence from PHP and create something similar to the "superglobals" variables.
To do this, you can create a new module specifically for storing all the global variables your application might need. For this you can create a function that will initialize any of these globals with a default value, you only need to call this function once from your main class, then you can import the globals file from any other class and use those globals as needed.
For example, let's create a function similar to the example above that will increment a global variable. For this we'll need 3 classes to prove the concept; a main class, a class containing our increment function and a class containing the globals.
globals.py
def initialize():
global num
num = 1
main.py
import globals
import test
if __name__ == "__main__":
globals.initialize()
print( globals.num ) # print the initial value
test.increment()
print( globals.num ) # print the value after being modified within test.py
test.py
import globals
def increment():
globals.num += 1
When we run main.py the output will be:
1
2
As you can see, once we've initialized the global variable in globals.py, we can then access 'num' as a property from any other module within the application and it will retain its value.
If you're having trouble understanding how this works, feel free to leave a comment below!