Global in Python | Global Keyword
Global in Python is a keyword that we use to make a variable global in the function. A global keyword allows us to modify a variable outside of the current scope.
The scope of local variable is limited to the function or block in which we specify it. Furthermore, when we define a variable within a function or block, that variable is local by default.
We can use it only within that function because it is not accessible outside. But, we can create a variable global within a function using global keyword. When we define a variable with a keyword global inside a function, we call it as global statement in Python.
Let’s take a simple example of a global statement using the global keyword to better understand the definition.
Example 1:
# Global variable inside a function.
def my_funct(): # function header.
global x; # global statement.
x = 50 # x is not local it is a global because of global keyword.
my_funct() # function calling.
print(x) # prints the value of global variable.
Output: 50
As you see in the above example program, we can easily access the global variable declared inside a function from outside the function.
Rules Used for Global Keyword in Python
The some basic rules for global keyword in Python that you should keep in mind. They are as follows:
- We use a global keyword to make a variable global inside the function.
- There is no need to use global keyword outside the function because it has no effect.
- Variables that are not referenced within a function, they are implicitly global by default.
Access and Modify Python Global Variable
Let’s write a program in which we will access a global variable from the inside of a function.
Example 2:
x = 10 # x is global variable.
y = 20 # y is a global variable.
def my_func():
# Accessing global variables inside the function.
print(x)
print(y)
my_func()
Output: 10 20
In this example, we can see that we have easily accessed global variables from the inside of a function. Now let’s write a program in which we will try to modify the global variable from inside a function.
Example 3:
x = 10 # x is global variable.
y = 20 # y is a global variable.
def my_func():
# Trying to modify the global variable inside the function.
x = x + 30
print(x)
print(y)
my_func() # function call.
Output: UnboundLocalError: local variable 'x' referenced before assignment
Here, we have created two global variables, namely x and y. Inside the function my_func(), we are updating the value of x by incrementing by 30. No updating is in the value of b.
Since we are trying to modify the global variable x inside the function without using global statement, therefore, it has displayed error. Because we can only access the global variable but cannot modify it inside the function.
To overcome this error, we need to use the global keyword. Using global statement, we can modify or update the value of global variable x inside the function. Let us try it.
Example 4:
x = 10 # x is global variable.
y = 20 # y is a global variable.
def my_func():
# Modify the global variable inside the function using global statement.
global x # global statement.
x = x + 30
print(x)
print(y)
my_func()
Output: 40 20
In the above program code, we have defined x as the global keyword inside the function my_func(). Then, we have incremented the variable x by 30, i.e. x = x + 30.
As we can see in the output while calling my_func(), the value of global variable x is modified from 10 to 40.
Note:
We cannot directly assign a value to a global variable within a function. Although, we can only declare reference with it. For example, global x.
Local Variable Having the Same Name As Global Variable
Let’s write a program in which we will define a local variable having the same name as that of global variable in the program. In such a case, Python creates a new local variable of that name, different from the global variable.
Example 5:
x = 50
def showMe():
x = 60
print('Inside function, x =',x)
showMe()
print('Outside function, x =',x)
Output: Inside function, x = 60 Outside function, x = 50
Example 6:
x = 10
def my_func1():
x = 20
print('Inside my_func1, x = %d'%x)
def my_func2():
global x
x = x + 30
print('Inside my_func2, x = %d' %x)
print('x starts at %d' %x)
my_func1()
print('After my_func1, x is now %d' %x)
my_func2()
print('After my_func2, x is now %d' %x)
Output: x starts at 10 Inside my_func1, x = 20 After my_func1, x is now 10 Inside my_func2, x = 40 After my_func2, x is now 40
In this example, first we have defined a variable x and assigned a value 10 to it. Then, we have defined two functions my_func1() and my_func2().
In the my_func1() function, we have defined a variable x and assigned the value of 20 to it. This variable x has a local scope, which is strictly only for use within this function.
Inside the my_func2() function, we have defined a variable x with a global keyword. Since we have used global keyword, it refers to the global variable, not local scope.
Now anything happens to the variable x within the routine will change in the value of variable declared in the first line of code.
Now the code continues and will print the statement “x starts at 10”. Then, calls function my_func1(), which creates its own variable x, assigns the value 20 to it and prints the output.
When the program control comes back from that, it prints “After my_func1, x is now 10”. Next, we have called the function my_func2().
Since we have declared that variable x in the function is the global one, it gets changed to 40 and printed. On return from the function, it prints “After my_func2(), x is now 40”.
Global in Nested Functions
In Python programming language, we can also use the global keyword within a nested function. Let’s take an example of it.
Example 7:
# Outer function.
def outer_func():
x = 30
# Inner function.
def inner_func():
global x
x = 60
print("Before inner_func() call, x is",x)
inner_func() # Inner function call.
print("After inner_func() call, x is",x)
# Outer function call.
outer_func()
print("Outside both function, x is",x)
Output: Before inner_func() call, x is 30 After inner_func() call, x is 30 Outside both function, x is 60
Here, we have declared a global variable inside the nested function inner_func(). Inside outer_func(), x has no effect of the global keyword. Before and after inner_func() function call, x takes the value of the local variable, i.e. x = 30.
Outside the outer_func() function, x will take the value defined in the inner_func() function, i.e. x = 60. Because we have used the global keyword with x to construct a global variable inside the inner_func() function (local scope).
Therefore, if we make any changes inside the inner_func() function, the changes will also appear outside the local scope, i.e. outer_func().
In this tutorial, you have learned about the global keyword in Python with the help of various example programs. I hope that you will have understood the basic points of global statement and practiced all example programs. Stay tuned with the next tutorial where you will understand lambda function in Python.
Thanks for reading!!!