Global keyword :
Global keyword allows for modifying the variable outside of its current scope. global keyword allows modifying the global variable inside the non-global space i.e inside the function.
The global keyword is used when we want to modify the value of the global variable inside the function.
Things to Remember :
- When a variable is declared outside the function, then by default it is the global variable. This can be accessed anywhere in the program, inside the functions, and in classes too.
- When a variable is declared inside a function, it is local by default. The variable can be accessed in function only, not anywhere else.
- We use global keywords to modify the value of a global variable inside the function.
- Using a global keyword outside the function doesn’t make sense, because by default all variables outside a function are global.
Use of global keyword :
We encounter many situations where we need to modify the value of a global variable inside the function, then the global keyword comes into the picture.
Let’s try to access the global variable inside the function
Code :
Python Code
a = 4 # global variable
# function
def func():
print(a)
func()
Output: 4
We can able to access the global variable inside the function.
Let’s try to modify the global variable without using the global keyword inside the function.
Code:
Python Code
a = 4 # global variable
# function
def func():
a = a +2 #modifying the global variable
func()
Output:
UnboundLocalError: local variable ‘a’ referenced before assignment.
Modifying the global variable inside the function raised the error. So we cannot modify the global variable inside the function without using the global variable.
Let’s try to modify the global variable inside the function using the global variable :
Code :
Python Code
a = 4 # global variable
print("Value of a before function call : ", a)
# function
def func():
global a #using global keyword
a = a + 2 # modifying the global variable
func()
print("Value of a after function call : ", a)
output:
Value of a before function call: 4
Value of a after function call: 6
In the above program, we defined ‘ a ’ as a global variable inside the function and tried to modify the value.
The value of ‘ a ’before and after the function call is different, which means the value of the global variable is modified in the function. Hence by using the global keyword we can change the value of the global variable inside the function.
NOTE: Inside the function, if we define a variable as global, then it can be accessed outside the function too.
Code :
Python Code
def func() :
global x
x = 30
func()
print(x)
Output: 30
In the above program, x is defined inside the function, even though we can able access the x outside the function, this is because x is defined as a global variable inside the function.
Global Variable in Nested Functions :
Code :
Python Code
def func1():
x = 30
def func2():
global x
x = 40
print("value of x before calling func2 : ", x)
func2()
print("value of x after calling func2 : ", x)
func1()
print("value of x outside the function :", x)
Output:
value of x before calling func2: 30
value of x after calling func2: 30
value of x outside the function: 40
In the above code, We declared a global variable inside the nested function func2(). Inside the fun1() ‘ x ’ has no effect on the global keyword.
‘ x ‘ takes the value of the local variable i.e x= 30 before and after calling fun2(). Outside the func1() the variable ‘ x ‘ had taken the value defined in the fun2() function i.e x = 40. This is because we have used the global keyword to create a global variable inside the func2() function (local scope). The changes made inside the fun2() function will appear outside the local scope. i.e outside the foo() function, but not in the local scope i.e inside the foo() function.
Special thanks to SaiSri Angajala for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article. If you want to suggest any improvement/correction in this article please mail us at [email protected]