Python Global Keyword
What is the global keyword in Python?
The global
keyword is a keyword that allows a user to change the variable outside of the current scope. It is used to create global variables from a non-global scope, e.g. inside a function.
Global keyword is used inside a function only when we want to change or assign a variable. Global keyword is not needed for accessing and printing.
Rules of global keyword
The basic rules for the global
keyword are as follows:
- When a variable is declared within the function's body, it is local by default unless explicitly declared as global.
- When a variable is defined outside of a function, it is global by default. You don't need to use the
global
keyword. - We use the
global
keyword to assign and change a global variable inside a function. - There is no need to use the
global
keyword outside a function.
Use of global keyword
There is no need to use the global
keyword to access a global variable inside a function.
Example: Accessing Global Variable From Inside a Function
In the following example, we will access a global variable from inside a function.
x = 5 # global variable
def my_func():
print(x)
my_func()
Output
5
As we can see above, we can access a global variable from inside a function without using the global
keyword.
Example: Modifying Global Variable From Inside a Function
In the following example, we will try to change a global variable from inside a function.
x = 5 # global variable
def my_func():
x = x + 3
print(x)
my_func()
Output
UnboundLocalError: local variable 'x' referenced before assignment
As we can see above, an UnboundLocalError
is raised because we can only access the global variable, but we cannot change the global variable from inside the function.
You can overcome this error by using the global
keyword.
Example: Modifying Global Variable From Inside a Function using the global keyword
In the following example, we will change a global variable from inside a function using the global
keyword.
x = 5 # global variable
def my_func():
global x
x = x + 3
print("Inside my_func():", x)
my_func()
print("In main:", x)
Output
Inside my_func(): 8
In main: 8
In the above code, inside the my_func()
function, we defined a variable x
using a global
keyword.
Then, we increment the variable x
by 3
, x = x + 3
.
After that, we called the my_func()
function in the main
scope, and we printed the global variable x
.
The printed value of the variable x
inside and outside the my_func()
function is the same, x = 8
.
Global Variables Across Python Modules
It is recommended to create a module config.py
to keep all global variables and shared information across Python modules in the same place.
Example: Share global Variables Across Python Modules
The following steps show how we can share global variables across the python modules.
Creation of a
config.py
file to store global variables.x = 7 y = "hi"
Creation of a
change.py
file to modify global variables.import config config.x = 9 config.y = "Hello World"
Creation of a
main.py
file to check changes in valueimport config import change print("x :", config.x) print("y :", config.x)
After running the
main.py
file the output will be9 Hello World
As we can see above, we have created three files: config.py
, change.py
, and main.py
.
The purpose of the module config.py
is to store the global variables x
and y
. In the change.py
, we import the change.py
module and change the values of x
and y
. In the main.py
file, we import both config.py
and change.py
modules. Finally, we print the values of the global variables x
and y
. As we can see, the value of x
and y
has changed from the initial values to the values assigned in the change.py
module.
Global in Nested Functions
To use global variables inside a nested function, we have to declare the variable with the global
keyword inside the nested function.
Example: Using a Global Variable in Nested Function
In the following example, we will see how we can use a global variable inside a nested function.
def func():
x = 10
def change():
global x
x = 30
print("Before calling change()", x)
print("Calling change() now")
change()
print("After calling change()", x)
func()
print("x in the main: ", x)
Output
Before calling change() 10
Calling change() now
After calling change() 10
x in the main: 30
In the above program, we declared a global variable inside the nested function change()
. Inside the local function func()
, the scope of x
is local, and it is not affected by the global
keyword declaration.
Before and after calling change()
function, the variable x
had the value of local variable x = 10
. Outside of func()
function, the value of the variable x
will be the value defined inside the change()
function i.e x = 30
. The reason for this behavior is because we have used the global
keyword to create x
as a global variable inside the change()
function.
Any modification to x
inside the change()
function willl appear outside the local scope, i.e. func()
.