Global and Nonlocal Declarations
Understanding how Python manages variable scope is essential for writing clear and correct code. Two important keywords, global and nonlocal, let you control which variables you are modifying when working with functions and nested functions.
Use the global keyword when you need to assign a value to a variable defined at the module (global) level from inside a function. Normally, assigning to a variable inside a function creates or updates a local variable. If you want your function to update a variable defined outside it, you must declare it as global. This is helpful for maintaining state or configuration that should persist across different parts of your code, but you should use it carefully to avoid making your code hard to understand or debug.
The nonlocal keyword, on the other hand, is used inside nested functions. It allows you to modify a variable defined in an enclosing (but non-global) scope. This is particularly useful in closures, where an inner function needs to update a variable from its containing function. Without nonlocal, assignments in the inner function would create a new local variable instead of modifying the one from the outer function.
12345678910# Using the global keyword to modify a global variable inside a function counter = 0 def increment(): global counter counter += 1 increment() print(counter) # Output: 1
The nonlocal keyword provides a way to access and modify variables in a function's enclosing scope, rather than the global scope. When you have a function defined inside another function, and you want the inner function to update a variable from the outer function, you use nonlocal. This helps maintain and update state across multiple calls to the inner function, which is a common pattern in closures and decorators. Without nonlocal, the inner function would only be able to read the variable, not assign to it.
12345678910111213# Demonstrating the use of nonlocal in nested functions def outer(): count = 0 def inner(): nonlocal count count += 1 return count return inner incrementer = outer() print(incrementer()) # Output: 1 print(incrementer()) # Output: 2
1. What does the global keyword do?
2. When should you use nonlocal in Python?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain the difference between global and nonlocal with more examples?
When should I use global vs nonlocal in my own code?
Are there any best practices or pitfalls to avoid when using these keywords?
Awesome!
Completion rate improved to 8.33
Global and Nonlocal Declarations
Swipe to show menu
Understanding how Python manages variable scope is essential for writing clear and correct code. Two important keywords, global and nonlocal, let you control which variables you are modifying when working with functions and nested functions.
Use the global keyword when you need to assign a value to a variable defined at the module (global) level from inside a function. Normally, assigning to a variable inside a function creates or updates a local variable. If you want your function to update a variable defined outside it, you must declare it as global. This is helpful for maintaining state or configuration that should persist across different parts of your code, but you should use it carefully to avoid making your code hard to understand or debug.
The nonlocal keyword, on the other hand, is used inside nested functions. It allows you to modify a variable defined in an enclosing (but non-global) scope. This is particularly useful in closures, where an inner function needs to update a variable from its containing function. Without nonlocal, assignments in the inner function would create a new local variable instead of modifying the one from the outer function.
12345678910# Using the global keyword to modify a global variable inside a function counter = 0 def increment(): global counter counter += 1 increment() print(counter) # Output: 1
The nonlocal keyword provides a way to access and modify variables in a function's enclosing scope, rather than the global scope. When you have a function defined inside another function, and you want the inner function to update a variable from the outer function, you use nonlocal. This helps maintain and update state across multiple calls to the inner function, which is a common pattern in closures and decorators. Without nonlocal, the inner function would only be able to read the variable, not assign to it.
12345678910111213# Demonstrating the use of nonlocal in nested functions def outer(): count = 0 def inner(): nonlocal count count += 1 return count return inner incrementer = outer() print(incrementer()) # Output: 1 print(incrementer()) # Output: 2
1. What does the global keyword do?
2. When should you use nonlocal in Python?
Thanks for your feedback!