7
Lesson 7
Local vs. global scope
In Python, variables can have different scopes, which determines where in your code they can be accessed. The two primary scopes are local scope and global scope. Understanding these is crucial for managing variables and avoiding bugs caused by unintentional variable changes.
1. Global scope:
A variable has a global scope if it is defined outside of any function or block. Global variables can be accessed from anywhere in the code, including inside functions (unless the variable is redefined within the function).
1. Global scope:
A variable has a global scope if it is defined outside of any function or block. Global variables can be accessed from anywhere in the code, including inside functions (unless the variable is redefined within the function).
# Global variable message = "Hello, World!" def greet(): print(message) greet() # Output: Hello, World! print(message) # Output: Hello, World!
Here, message is defined outside of any function, so it’s a global variable. The greet() function can access message because it’s in the global scope.
Modifying global variables inside functions
If you need to modify a global variable inside a function, you must explicitly declare it as global. Without this, Python will treat any assignment to that variable within the function as creating a new local variable.
count = 10 # Global variable def increment_count(): global count count += 1 increment_count() print(count) # Output: 11
Here, using the global keyword tells Python that count inside the function refers to the global count variable, allowing us to modify it.
2. Local scope:
A variable has a local scope if it is defined within a function. Local variables are only accessible inside the function where they are created; they don’t exist outside of that function.
def greet(): message = "Hello from the function!" print(message) greet() # Output: Hello from the function! # Trying to accessmessage outside the function will raise an error print(message) # Error: NameError: name 'message' is not defined
In this example, message is defined inside the greet() function, making it a local variable. It only exists during the execution of greet() and is inaccessible outside that function.
3. Nested functions and enclosing scope:
Python also has enclosing scope when functions are nested within each other. In this case, an inner function can access variables in the enclosing (or outer) function, but it still can’t modify them directly unless you use the nonlocal keyword.
def outer_function(): outer_message = "Hello from the outer function" def inner_function(): print(outer_message) # Accessing the enclosing scope inner_function() outer_function() # Output: Hello from the outer function
Here, outer_message is defined in the outer function’s scope, and inner_function() can access it because it’s in an enclosing scope. However, outer_message is still not global, so it’s only accessible within outer_function().
Modifying enclosing scope variables with nonlocal
If you want to modify an enclosing variable inside an inner function, use the nonlocal keyword.
def outer_function(): counter = 0 def inner_function(): nonlocal counter counter += 1 print(counter) inner_function() inner_function() outer_function() # Output: # 1 # 2
Here, nonlocal counter allows inner_function() to modify counter in the enclosing outer_function() scope.
Summary:
- Global scope: Variables defined outside any function, accessible throughout the entire code.
- Local scope: Variables defined within a function, accessible only inside that function.
- Enclosing scope: Variables in an outer function are accessible in an inner function, but to modify them, you must use nonlocal.
- Global keyword: Allows you to modify a global variable inside a function.
- Nonlocal keyword: Allows you to modify an enclosing variable inside a nested function.
Understanding scopes is essential for managing variables correctly in your code. Local scope keeps functions self-contained, which reduces the chance of accidentally modifying global variables. Global variables are accessible throughout the code, but their modification should be limited to avoid side effects.