What is the global keyword?
The global keyword is used to create or update a global variable from a nonglobal scope (such as within a function or class).
This means that the global keyword allows the variable to be modified outside the current scope. It is used to create a global variable and make changes to the variable in a local context.
Syntax
The syntax of the global keyword is as follows:
global var1,var2,…where var1, var2, ... are identifiers for variable names.
Rules of global keyword
The rules for the global keyword in Python are:
- A variable created within a function is local by default.
- A variable created outside a function is global by default. It is not necessary to use the
globalkeyword. - The
globalkeyword is used to read and write a global variable within a function. - Using the
globalkeyword outside of a function has no effect.
Examples
Modifying Global Variable Inside a Function
A variable declared outside all functions has a global scope. This means that it is accessible throughout the file and also within any file that imports it.
Consider the following (wrong) example:
my_var = 1 # global variable
def increment():
my_var = my_var + 1
print(my_var)
increment()The program will show an error:
UnboundLocalError: local variable 'my_var' referenced before assignmentTherefore, the correct way to modify a global variable within a function is as follows:
my_var = 1 # global variable
def increment():
global my_var # declare my_var global
print(my_var)
my_var = my_var + 1 # change value of my_var
print(my_var)
increment()
print(my_var)Output
1
2
2Create Global Variable Inside a Function
When you declare a variable global, it is added to global scope, if not already present.
For example, declare x global inside a function and access it outside the function:
def my_function():
global x # x is global
x = 123
my_function()
print(x) # 123Output
123Global Variables in Nested Functions
def outer_function():
x = 10
def inner_function():
global x
x = 20
print(f"Before calling inner_function: {x}")
inner_function()
print(f"After calling inner_function: {x}")
outer_function()
print(f"x in main: {x}")Output
Before calling inner_function: 10
After calling inner_function: 10
x in main: 20If changes are made within the inner_function() function, the changes appear outside the local scope (thus also in outer_function().
This is because the global keyword is used in x to create a global variable within the inner_function() function.
Global Variables Across Python Modules
In Python, a single module, called for example config.py, can be created to contain global variables and share information among Python modules in the same program.
Let's see how with an example.
Create a config.py file that stores global variables:
my_number = 0
my_string = "a string"Create a updater.py file to change global variables
import config
config.my_number = 123
config.my_string = "another string"Create a main.py file print the variables
import config
import update
print(config.my_number)
print(config.my_string)Output
123
another string