Overview: global Keyword in Python
A function may need to change the value of a global variable. When a function assigns a value to a variable, Python normally treats that variable as local to the function.
Python provides the global keyword to handle this situation.
What Is the global Keyword in Python?
The global keyword tells Python that a variable inside a function refers to a variable in the global scope rather than a local variable.
It is mainly used when a function needs to modify a global variable.
Syntax of the global Keyword
Use the global keyword followed by the name of the global variable you want to modify inside a function.
def function_name():
global variable_name
variable_name = new_value
Explanation: The global keyword tells Python that variable_name refers to the global variable rather than a local variable inside the function.
Modifying a Global Variable Inside a Function
A function can modify a global variable by using the global keyword before assigning a new value to it.
count = 0
def increase():
global count
count = count + 1
increase()
print(count)
# Output:
1
Explanation:
- The
globalkeyword tells Python that the namecountinside the function refers to a variable in the global scope. count = count + 1increases its value from0to1.print(count)displays the updated value.
Creating a Variable Inside a Function
When a function creates a variable, Python normally treats it as a local variable. The global keyword tells Python to use the global scope instead.
Let’s understand these concepts with the examples below.
Without the global Keyword
def set_language():
language = "Python"
set_language()
print(language)
# Error:
NameError: name 'language' is not defined
Explanation:
language = "Python"creates a local variable insideset_language().- The local
languagevariable exists only insideset_language(). print(language)outside the function cannot access the local variable, so Python raises aNameError.
With the global Keyword
def set_language():
global language
language = "Python"
set_language()
print(language)
# Output:
Python
Explanation:
global languagetells Python thatlanguagebelongs to the global scope.language = "Python"creates the globallanguagevariable and assigns it the value"Python".print(language)displays the value of the global variable.
When the global Keyword Is Not Needed
A function can access the value of a global variable without using the global keyword. Here, name is a global variable because it is created outside the function.
name = "Alice"
def greet():
print(name)
greet()
# Output:
Alice
Explanation: Here, the greet() function accesses the value of name using print(name). Since the function only accesses the value and does not assign a new value to name, it does not need the global keyword.
Uses of the global Keyword in Python
The global keyword is used when a function needs to work with a variable in the global scope.
- Modifying a global variable inside a function
- Creating a global variable from inside a function
Practical Examples: Global Keyword in Python
These examples show how the global keyword can be used in different situations to update shared values.
Example 1: Function Counter Using a Global Variable
count = 0
def increase():
global count
count += 1
return count
print(increase())
print(increase())
print(increase())
# Output:
1
2
3
Explanation: The function uses global to update the same count variable each time it is called.
Example 2: Updating a User Score
score = 0
def add_points(points):
global score
score += points
add_points(10)
add_points(5)
print(score)
# Output:
15
Explanation: The add_points() function uses global to update the same score variable each time it is called.
Example 3: Changing a Program Setting
mode = "Normal"
def enable_dark_mode():
global mode
mode = "Dark"
enable_dark_mode()
print(mode)
# Output:
Dark
Explanation: The enable_dark_mode() function uses global to change the program’s mode setting. The updated value can then be accessed outside the function.
Common Mistakes With the Global Keyword
Here are some common mistakes to avoid when using the global keyword in Python.
1. Modifying a Global Variable Without global
count = 10
def increase():
count += 1
increase()
# Error:
UnboundLocalError
Explanation: Because count is assigned inside the function, Python treats it as a local variable. The function therefore cannot use count += 1 to modify the global variable without global.
2. Using global When Only Reading a Global Variable
count = 10
def show_count():
global count
print(count)
show_count()
# Output:
10
Explanation: The global keyword is unnecessary here because the function only reads count. A function can read a global variable without using global.
Note: Learning about local and global variables will help you see how the global keyword works with different scopes. See the related topics below to learn more.
Related Topics
The global keyword is closely connected with local and global scope. The shared scope concepts are covered separately.
Key Takeaways: Global Keyword
Here are the key points to remember about the global keyword in Python.
- The
globalkeyword tells Python that a name refers to the global scope. - It is needed when a function assigns to a global variable.
- A function can read a global variable without
global. - The
globalkeyword can also create a global variable from inside a function.