Local vs. Global Scope (LEGB Rule)

You're debugging a Python script that processes user data, and you've hit a puzzling issue. A function update_user_count() is designed to increment a global counter, but no matter how many times you call it, the counter outside the function remains stubbornly at its initial value. It feels like the function is completely ignoring the global variable. Why isn't your function affecting the counter outside its own body, and how does Python decide which variable you're actually referring to?

When Functions Create Their Own Worlds

This scenario is a classic example of a scope misunderstanding. When you assign a value to a variable inside a function, Python, by default, assumes you are creating a new variable that exists only within that function's boundaries. This new variable shadows any variable of the same name that might exist outside, leading to unexpected behavior where your function appears to have no effect on the outside world.

pythonThe Pitfall: Unintended Local Variable Creation

The UnboundLocalError in the example highlights a crucial aspect of Python's variable handling. When you use an assignment operator (=, +=, -=, etc.) for a variable name within a function, Python automatically assumes you are creating a new local variable with that name. It does not look for a variable of the same name in an outer scope for modification unless explicitly told to do so. The error occurs because the right side of user_count + 1 tries to read user_count before the local user_count has been assigned a value.

Local Scope
The region of a program where a variable is defined and accessible only within a specific function or block of code. Variables in local scope are created when the function is called and destroyed when the function finishes execution.
Example: In def greet(): message = 'Hello', message exists only inside greet().
pythonLocal Variables are Isolated

Global Scope: Variables for Everyone (Read-Only by Default)

Global Scope
The outermost region of a Python program, where variables are defined outside of any function. Variables in global scope are accessible from anywhere in the program, including inside functions, but cannot be directly modified (assigned to) from within a function without special declaration.
Example: app_name = 'DataProcessor', app_name is a global variable.
pythonReading Global Variables from Functions
Check Your Understanding
What will be the value of x printed outside the function after modify_x() is called?

Python's Search Party: The LEGB Rule

When Python encounters a variable name, it doesn't just guess where to find it. It follows a strict, hierarchical search order known as the LEGB Rule. This rule dictates the sequence in which Python looks for a variable: first in the Local scope, then in any Enclosing function scopes, next in the Global scope, and finally in the Built-in scope. Understanding this order is key to predicting how your variables will behave.

The LEGB Rule: Python's Variable Lookup Order
Loading diagram...
This flowchart illustrates Python's systematic approach to resolving a variable name, moving from the most specific (Local) to the most general (Built-in) scope.

Let's briefly elaborate on each part of the LEGB rule:
- L (Local): This is the innermost scope, specific to the current function being executed. Any variables assigned within this function (unless declared global or nonlocal) reside here.
- E (Enclosing Function Locals): This refers to the scope of an outer function when you have nested functions. Variables defined in the outer function are accessible to inner functions.
- G (Global): This is the module-level scope. Variables defined directly in the main body of a Python script or module are global.
- B (Built-in):: This is the broadest scope, containing Python's pre-defined names like print(), len(), True, False, etc. These are always available.

Breaking the Walls: The global Keyword

While Python's default behavior protects global variables from accidental modification, there are times when you genuinely need to change a global variable from within a function. This is where the global keyword comes in. By explicitly declaring a variable as global inside a function, you tell Python: 'Do not create a new local variable; instead, I intend to refer to and modify the variable of this name that exists in the global scope.'

pythonModifying a Global Variable with `global`
⚠️ Use `global` Sparingly

While powerful, overusing the global keyword can make your code harder to read, debug, and maintain. Functions that modify global state are said to have side effects, which can lead to unexpected behavior if not managed carefully. Often, it's better to pass variables as arguments to functions and return new values, rather than directly modifying globals.

Try It Yourself
Modify the process_data function below. Currently, it tries to update a global processed_records_count but fails. Use the global keyword to ensure that each call to process_data correctly increments the global counter by the records_processed value.
python
Key Takeaways
  • Variables defined within a function exist in local scope and are inaccessible outside that function.

  • Variables defined at the module level exist in global scope and are readable from anywhere in the program.

  • By default, assigning to a variable inside a function creates a new local variable, even if a global variable with the same name exists.

  • Python uses the LEGB Rule (Local, Enclosing, Global, Built-in) to determine the order in which it searches for a variable's definition.

  • To explicitly modify a global variable from within a function, you must use the global keyword before the first assignment to that variable.

  • While global is necessary for modification, use it judiciously to avoid complex side effects and maintain code clarity.

  • The initial debugging mystery of the user_count not incrementing was due to Python creating a local user_count instead of modifying the global one, a behavior now understood through scope rules.

← All lessons in Functions & Scope

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free