There are many types of variables. Based on its datatype, there are mainly three types of variables – predefined, derived, and user-defined. Python has a well-structured format for maintaining variables. Python describes all the variables as objects. There’s another basis on which variables can be differentiated – its scope. Based on their scope, the variables can be differentiated into 3 groups, local variables, global variables, and environment variables.
In this article, we’re going to understand the global variables and their functioning.
Types of variables based on their scope
Based on the scope, there are mainly 3 types of variables, global, local, and environment variables. The scope of the variable basically means where you can access the variable.
Related: Learn about types of variables in depth.
Local variables are the variables that are declared inside a function. The scope of the local variables is limited to that specific function only. It can store any object – integer, string, etc. You cannot access local variables inside a function. If you try to access a local variable outside of the function it’s part of; the interpreter will throw an error.
Global variables are the ones that are not part of a specific function and are declared in a module globally. You can access them anywhere in the entire module. You can even access them in other Python files by importing them.
Environment variables are different from other variables. They are the same for all the Python files in the entire environment. They are language-independent and also can be accessed in the terminal using Bash/Shell commands. The most commonly used environment variable is Pythonpath. It stores the paths to directories where the Python interpreter is supposed to look for files requested to import. Using the os or sys module, you can access and modify them in any Python file. Using the shell commands, you can also access and modify them in the terminal itself.
Related: Learn more about environment variables.
Accessing global variables
Global variables in Python can be tricky sometimes. Let’s see how we can access it in different positions in a module.
Accessing global variables globally
First, we’ll have to declare a global variable. To declare a variable in global scope, make sure the variable isn’t part of any function. A variable is declared as a global variable if it doesn’t have any indentation. Later, you can access it anywhere in the entire module by its name.
glob_var = 1 print(glob_var) glob_var = 2 print(glob_var)
In the above block of code, we declared a global variable glob_var and set its value as 1. Then we printed its value in the global scope, so we got the output as 1. Then we changed its value to 2 and then printed it again, which gave us the output as 2.
Accessing global variables in a function’s scope
We can also access this variable inside a function. Let’s see how we can do it.
glob_var = 1 def fun(): print(glob_var) fun()
So same as before, we declared a global variable. This time we accessed and printed it in a function named
fun. After that, we simply called this function. The output came as expected. There were no errors.
Now, let’s try to change this global variable within a function.
glob_var = 1 print("glob var initially:",glob_var) def fun(): glob_var = 2 print("glob var after modifying in a function:",glob_var) fun() print("glob var out of the function's scope:",glob_var)
So when we changed the value of the
glob_var within the function
fun, we can see that the value of
glob_var became 2 within the scope of the function
fun but when we tried to print it out of the function
fun it still printed 1. Why so?
Why are we not able to modify the global variable permanently inside of a function?
Let’s try to properly understand what’s exactly happening here. First, we created a global variable
glob_var and set its value to 1. Then we created a function fun, in which we set glob_var’s value as 2. So now, when we checked, the
glob_var's value was 2 within the
fun function. But it hasn’t changed. We know that cause when we checked the value of
glob_var outside the scope of
fun, it was the same as before.
The reason behind this anomaly is that when we set the value of
glob_var as 2 in the
fun function, it created a new local variable
glob_var instead of modifying the global variable. So that is the reason why when we printed its value within the scope of the function
fun, it showed a value as 2 cause it accessed the LOCAL variable, but when we printed out of the function it printed the value as 1 as now the local variable is out of the scope so it again accessed the GLOBAL variable.
How to modify or reset the global variable permanently inside of a function?
Now that we understand the anomaly, how can we change the actual global variable
So the logic behind this is when you try to read a global variable inside a function, you’re able to read it. But when you try to write a global variable inside a function, it won’t write it but instead create a new local variable of the same name.
To overcome this phenomenon, we need to specify that we want to access the global variable
glob_var. To do so, we need to use the
GLOBAL keyword before accessing the global variable.
glob_var = 1 print("glob var initially:",glob_var) def fun(): global glob_var glob_var = 2 print("glob var after modifying in a function:",glob_var) fun() print("glob var out of the function's scope:",glob_var)
So we did the same thing as before with a single extra line of code before the
glob_var=2 line. Using the
global keyword, we specified that the
glob_var variable that we’re about to access is supposed to be a global variable. And that’s it, and now it changed the global variable
Now many times when you’re programming, you must have noticed the following type of error.
glob_var = 1 def fun(): if glob_var==1: glob_var = 2 fun()
Here, even though we have declared the
glob_var before, we’re getting a reference before the assignment error in the line where we’re reading the
glob_var. But we just learned that we can read a global variable in a function. The reason is that if the if block we’re trying to modify it. If we don’t write on it, it won’t throw an error. So instead of writing, let’s try to print the
glob_var = 1 def fun(): if glob_var==1: print(glob_var) fun()
Now there’s no error. So the above error can be dealt with again in a similar way as before. We can use the
global keyword to access the global variable.
Global variables in Python can be tricky sometimes. With such a simple and short syntax, Python comes with a few ambiguous situations. Python has ways to deal with them, but it can be irritating if you don’t know them. Knowing when to use global variables and not is important. So consequently, we must also know how to manipulate or reset these variables. Make sure you keep practicing.
Stack Overflow answer for the same question.