8
Lesson 8
Global keyword
The global keyword in Python is used to declare that a variable inside a function refers to a variable defined in the global scope. This allows you to modify a global variable from within a function. Without the global keyword, any assignment to a variable name inside a function creates a new local variable instead of modifying the global one.
1. Defining global variables:
A global variable is defined outside of any function, making it accessible throughout the entire script.
1. Defining global variables:
A global variable is defined outside of any function, making it accessible throughout the entire script.
# Global variable x = 10 def print_x(): print(x) print_x() # Output: 10
In this example, x is a global variable, and the print_x() function can access it directly.
2. Modifying global variables:
To modify a global variable inside a function, you must use the global keyword. If you do not declare the variable as global, Python will treat it as a local variable, leading to a NameError if you try to access it before assigning a value.
# Global variable counter = 0 def increment(): global counter # Declare that we want to use the global variable counter += 1 # Increment the global variable increment() print(counter) # Output: 1 increment() print(counter) # Output: 2
In this example:
- The increment() function declares counter as global, allowing it to modify the global counter variable.
- Each time increment() is called, the global counter is increased by 1.
3. Without the global keyword:
If you try to assign a value to a variable without declaring it as global, Python will create a new local variable instead of modifying the global variable.
# Global variable value = 5 def change_value(): value = 10 # This creates a new local variable named 'value' print(value) change_value() # Output: 10 print(value) # Output: 5 (the global variable remains unchanged)
The change_value() function creates a local variable value, which shadows the global variable.
The global variable value remains unchanged when you print it after calling change_value().
Key Points to remember:
Key Points to remember:
- Use of global: The global keyword allows functions to modify a global variable.
- Local vs. global scope: Without global, any assignment to a variable inside a function will create a local variable that does not affect the global variable.
- Best practices: While using global variables can be useful, it's generally good practice to minimize their use to avoid side effects and maintain cleaner, more maintainable code. If you find yourself needing to use global variables frequently, consider passing parameters to functions instead.
The global keyword is essential for modifying global variables within functions in Python. It provides a way to specify that a variable should refer to a variable defined in the global scope, allowing changes to persist outside the function. Understanding when and how to use global helps avoid common pitfalls associated with variable scope and improves code clarity.