Global variables in python
In Python, considering the scope, variables are categorized into global and local variables. In this article, we will discuss these two types along with examples.
Any variable holds a value in it. Depending on the place, the variable is declared in the program, and the variable can either hold the value until the end of the program or hold only until the end of a particular block. This is the differentiation between global and local variables.
A variable that is not defined inside a function and can hold its value till the end of the program, and can be accessed anywhere is called a global variable. On the other hand, the variable that is defined inside a particular function and can hold their values in that function block only are called local variables.
Once initialized, a global variable can be accessed throughout the program, even inside functions.
- Let us take an example:
In the following code:
def function ():
s = "Local variable"
print (s)
function ()
Output:
Local variable
We defined a variable s inside the function and accessed it. Hence, we were able to access it. If we try to access it outside the function:
def function ():
s = "Local variable" //Local variable
print (s)
function ()
print (s)
Output:
NameError: name 's' is not defined
Here, s is a local variable. Hence, its scope is permitted only to the function block.
- Let us now take an example of a global variable:
s = "Local variable" //Global variable
def function ():
print (s)
function ()
print (s)
Output:
Local variable
Local variable
Here, s is defined globally and not inside any particular code block. Hence, when we tried to access the value of s inside the function and outside the function, we could access it.
- Now, you might be wondering what if we declare a global variable and a local variable with the same name. What will happen if we modify the value of the local variable? Which value will be displayed when we try to access the variable inside the function?
Let’s see with this example:
s = "Global variable"
def function ():
s = "Local variable"
print (s)
function ()
print(s)
Output:
Local variable
Global variable
Understanding:
As you can observe in the program, a local variable dominates the global variable inside a function. Hence, when we tried to access s inside the function, the local variable s is accessed and not the global variable s.
- Now, say that we have a global variable s. We want to modify its value by writing a function. Can we do that? Let us see:
s = "Global variable"
def function ():
s = "I am a" + s
print (s)
function ()
Output:
UnboundLocalError: local variable 's' referenced before assignment
Understanding:
As discussed earlier, inside a function local variable dominates the global variable. Hence, in the above code, when we tried to modify the global variable inside the function, the variable is considered as a local variable. An error is raised asking us to define it first before modifying it. This problem occurs only when we need to assign or change the variable.
So, we need to let the interpreter know that we need to modify the global variable inside the function. But, how do we do that?
Global Keyword:
We need to mention the global keyword before the variable name to let the interpreter know that we talk about the global variable and not the local variable.
Let us take an example:
s = "Global variable"
def function ():
global s
s = "I am a " + s
print (s)
s = "Assigning a new value"
function ()
print (s)
Output:
I am a Global variable
Assigning a new value
Understanding:
In the above program, we declared a global variable s. We wrote a function in which we wanted to add I am a to s. To modify s, a global variable, we used the global keyword to let the interpreter know. Then, we concatenated "I am a "to s and printed it. This way, we modified the global variable inside a function. We changed it as s is declared as a global variable already. When we printed the value of s outside the function, we got the changed value as the output.
Program for clarity:
//Simple global variable
s = "Global variable"
def function ():
print (s)
function ()
print (s)
//simple local variable
def function1 ():
s = "Local variable"
print (s)
function1 ()
//modifying a global variable
t = "global variable"
def function2 ():
global t
t = "I am a " + t
print (t)
function2 ()
print (t)
Output:
Global variable
Global variable
Local variable
I am a global variable
I am a global variable