DEV Community

Samiksha Srivastav
Samiksha Srivastav

Posted on

global and nonlocal in Python (The Concept That Finally Made Scope Click for Me)

While learning Python scopes and closures, I reached two keywords that looked very similar initially:

  • global
  • nonlocal

At first, both felt like:
"Okay... both are just used to access outer variables."

But once I understand how Python internally handles variable assignment and scope, the difference became much clearer.

So here's my attempt to explain them in the simplest way possible.

First, Why Does This Error Happen?

Let's start with this example:

This gives:

UnboundLocalError

At first, this feels confusing because: x already exists globally.
So why is Python complaining?

What Python Internally Thinks

Whenever Python sees:

x = something
inside a function, it automatically assumes:

So internally this becomes:
local x = local x + 1

And now the problem becomes obvious:

  • the local x does not exist yet
  • but Python is trying to use it first

Hence: UnboundLocalError

Enter global

Now look at this:

Output:
11

What Did global Actually Do?

global x tells Python:

Do not create a local variable.
Use the global one instead.

So now the actual global variable gets modified.

Important Thing Most Beginners Miss

You do NOT need global to READ a global variable.

This works perfectly fine:

**x = 10

def test():
print(x)**

But:
the moment you MODIFY it,
Python tries creating a local variable.

That’s where global becomes necessary.

Now Comes nonlocal

This one confused me even more initially
Because it looks similar to global, but works differently.

Example

Again: UnboundLocalError

Same reason:
Python thinks x is local to inner().

Enter nonlocal

Output: 11

What nonlocal Actually Means

nonlocal x tells Python:

This variable belongs to the enclosing function scope.
Do not create a local copy.

So instead of creating a new local variable:
- Python modifies the outer function’s variable directly.

The Core Difference

  • global Targets: global scope
  • nonlocal Targets: enclosing function scope

Visual Understanding

x = "global"

def outer():

y = "outer"

def inner():

    z = "inner"
Enter fullscreen mode Exit fullscreen mode

Here:

  • x --> global scope
  • y --> enclosing scope
  • z --> local scope

So:

  • global works with x
  • nonlocal works with y

Why nonlocal Matters So Much

Because closures depend on it.

Example:

def counter():

count = 0

def increment():

    nonlocal count

    count += 1

    return count

return increment
Enter fullscreen mode Exit fullscreen mode

Now:

c = counter()

print(c())
print(c())
print(c())

Output:

1
2
3

The inner function keeps remembering and modifying the outer variable.

That’s where closures and nonlocal connect beautifully.

One-Line Understanding

global --> modifies global scope variable
nonlocal --> modifies enclosing function variable

Final Thoughts

Initially, both keywords looked almost identical to me.

But once I understood:

- how Python creates local variables,
- how assignment works internally,
- and how scopes are connected,
Enter fullscreen mode Exit fullscreen mode

the difference became much more intuitive.

And honestly, understanding these concepts made closures and Python scopes feel way less “magical” and much more logical.

Top comments (0)