AlgoMaster Logo

global & nonlocal

Last Updated: January 3, 2026

5 min read

Functions in Python are powerful tools that allow us to encapsulate logic and create reusable code.

However, as we dive deeper into functions, we encounter scenarios where we need to manipulate variable scope beyond the local context. That’s where global and nonlocal come in.

These keywords can be pivotal in controlling where variables are declared and accessed, which is crucial for writing clean, efficient code.

What is the global Keyword?

The global keyword is used to declare that a variable inside a function refers to a variable defined in the global scope. When you need to modify a global variable from within a function, this keyword is essential.

Declaring Global Variables

Let’s start with a simple example. Suppose you have a global variable that tracks a count:

In this code, we declare count as a global variable inside the increment function. Without the global keyword, Python would treat count as a local variable and raise an error when trying to modify it.

Why Use global?

Using global can be handy when you want to maintain state across function calls without passing variables explicitly. However, using global variables can lead to code that is hard to understand and maintain because it introduces side effects that can be difficult to track.

Real-World Application

Imagine you are creating a simple counter for web page visits. Using a global variable allows you to keep track of the visits across different function calls:

While this works, remember that overusing global variables can lead to tangled code. You might find it better to encapsulate state within classes or use other design patterns as your application grows.

The nonlocal Keyword Explained

The nonlocal keyword, introduced in Python 3, serves a different purpose. It allows you to assign values to a variable in an enclosing scope that is not global. This is particularly useful in the context of nested functions.

Understanding Nested Functions

Let’s look at how nonlocal works with nested functions. Consider the following example:

In this case, the inner_function modifies the count variable declared in outer_function. Without nonlocal, Python would treat count as a local variable within inner_function, leading to an error when you try to modify it.

When to Use nonlocal

Using nonlocal is particularly useful in closures, where you want to keep track of state without using global variables. This can help maintain clean code and encapsulate logic.

Real-World Application

Imagine you are creating a simple state machine that tracks the status of different processes:

This example demonstrates how nonlocal allows you to maintain the state of state across calls to change_state, all while keeping the function encapsulated.

Differences Between global and nonlocal

While both global and nonlocal serve to manipulate variable scope, they do so in different contexts. Here’s a quick breakdown:

Scope:

  • global is for variables in the global scope.
  • nonlocal is for variables in an enclosing (but non-global) scope.

Usage:

  • Use global when you need to modify a global variable from a function.
  • Use nonlocal when you need to modify a variable in an enclosing scope within a nested function.

Example Highlighting Differences

Here’s a contrasting example to illustrate the differences:

In this example, inner uses global to change the value of x in the global scope, while the outer function's x remains unchanged.

Common Pitfalls and Best Practices

Using global and nonlocal can introduce complexity into your code. Here are some common pitfalls to look out for:

Pitfall 1: Unintended Side Effects

Overusing global variables can lead to code that behaves unpredictably. It can be hard to track which function modifies a global variable, leading to bugs.

Pitfall 2: Confusion with Scope

When using nonlocal, it’s easy to confuse it with global. Always be aware of the variable’s scope and ensure you’re using the correct keyword.

Best Practices

  • Limit Global Variables: Use global variables sparingly. Consider passing variables as parameters or encapsulating them in classes.
  • Use nonlocal for Closures: When working with closures, prefer nonlocal to maintain state without polluting the global namespace.
  • Comment Your Code: When using global or nonlocal, add comments to clarify why you’re manipulating scope. This helps others (and your future self) understand your intent.

Summary

In this chapter, we explored the global and nonlocal keywords in Python, focusing on their purposes and differences. We looked at practical examples and discussed real-world applications, as well as common pitfalls to avoid.

Understanding these concepts enhances your ability to manage variable scope effectively and write cleaner, more maintainable code.

Now that you understand how to use global and nonlocal to manage variable scope effectively, you're ready to explore closures.

In the next chapter, we will look at how closures allow you to encapsulate state in a more elegant way, providing powerful patterns for your Python functions.