Back to all tutorials
pythonfunctionsscopepython-functions-series

Python Variable Scope Explained Like an Office Building

Understand local, global, and nonlocal scope in Python using a simple office building analogy. Includes the global and nonlocal keywords.

The Spark Engineer 2026-08-25 5 min read

Python Variable Scope, Explained Like an Office Building

Imagine an office building. Each employee has a private desk where they keep their own notes, nobody outside that desk can see them. There's also a shared bulletin board in the lobby that everyone in the building can read and, with permission, write on. Python variables work the same way: some live privately inside a function, and some are shared across the whole program.

Local Scope: The Private Desk

A variable created inside a function only exists inside that function. Once the function finishes, the variable is gone, like notes on a desk that get cleared at the end of the day.

Python
def take_notes():    note = "Buy milk"    print(note) take_notes()      # Buy milkprint(note)       # NameError: note is not defined

note never existed outside take_notes(). This is called local scope, and it's the default for anything defined inside a function.

Global Scope: The Lobby Bulletin Board

A variable created outside any function, at the top level of your script, is a global variable. Any function can read it, the same way any employee can walk by and read the lobby bulletin board.

Python
company_name = "Acme Inc." def greet():    print(f"Welcome to {company_name}") greet()

The Catch: You Can Read Globals, But Not Modify Them by Default

Reading the bulletin board is fine, but trying to scribble on it without permission can be problematic.

Python
counter = 0 def increment():    counter += 1   # UnboundLocalError increment()

Python assumes that if you assign to a variable inside a function, you're creating a new local variable, even if the global variable has the same name. To actually change a global variable, you must explicitly state so.

The global Keyword: Asking for Write Permission

Python
counter = 0 def increment():    global counter    counter += 1 increment()print(counter)  # 1

The global keyword tells Python "don't create a local copy, use the one from the lobby board."

Use this sparingly, functions that quietly change global state are harder to debug, the same way a building gets chaotic if everyone edits the shared board constantly.

Nested Functions and the nonlocal Keyword

Things get one level trickier with a function inside a function, think of it as a private meeting room inside someone's office. A variable in the outer function isn't local to the inner one, but it isn't global either.

Python
def outer():    count = 0     def inner():        nonlocal count        count += 1     inner()    inner()    print(count)  # 2 outer()

nonlocal tells Python "look one level up, not all the way to the global scope." Without it, inner() would create its own separate local count, and the outer one would never change.

The Scope Lookup Order (LEGB)

When Python looks up a variable name, it checks in this order:

  1. Local — inside the current function
  2. Enclosing — any outer function (relevant for nested functions)
  3. Global — the top level of the script
  4. Built-in — Python's own reserved names, like len or print

This is often abbreviated as LEGB, and it's the exact order the office analogy follows: check your own desk first, then the meeting room, then the lobby board, then the building directory.

What's Next

Scope explains where a variable lives. The next tutorial in this series tackles recursion, what happens when a function calls itself, and why that isn't as strange as it sounds.

...
Share

Discussion & Thoughts

Join the conversation with your Google Account

0 Comments

Sign in to leave a comment

We use 1-click Google authentication to prevent bot spam and verify authentic developer discussions.

Loading discussions...