Understanding Variable Scope and the Global Keyword in Python

Variable Scope Fundamentals

In Python, identifier visibility is determined by where a name is assigned. Variables declared at the module level reside in the global namespace, making them accessible throughout the entire script. When a function references a name without assigning a new value to it, the interpreter automatically searches outward to resolve it in the enclosing global scope.

def compute_total(offset):
    print("Sum:", base + offset)

base = 10
compute_total(5)

The code above successfully outputs Sum: 15 because base is fetched from the module level.

Conversely, identifiers created inside a function body or passed as parameters belong to the local namespace. They are initialized when execution enters the function and are destroyed upon return, isolating them from the rest of the program.

def compute_total(offset):
    base = 10
    print("Sum:", base + offset)

compute_total(5)

This variant yields the same result, but relies entirely on the locally defined base.

Demonstrating Scope Isolation and Name Conflicts

Python's strict separation of namespaces becomes apparent when identical identifiers appear in different scopes. Printing the variable after a function call clarifies wich version was used during execution.

When a function only reads a name, it references the global instance:

def compute_total(offset):
    print("Sum:", base + offset)

base = 10
compute_total(5)
print("External base:", base)

Output:

Sum: 15
External base: 10

If a variable is assigned in side the function, Python classifies it as local for the entire scope. Attempting to access that name from the module level triggers a NameError because it was never established globally:

def compute_total(offset):
    base = 10
    print("Sum:", base + offset)

compute_total(5)
print("External base:", base)

Output:

NameError: name 'base' is not defined

To explicitly observe how local and global namespaces operate independently, define the same identifier at both levels. The function will prioritize the local assignment, leaving the module-level variable unchanged:

def compute_total(offset):
    base = 10
    print("Sum:", base + offset)

base = 200
compute_total(5)
print("External base:", base)

Output:

Sum: 15
External base: 200

Modifying Global State with the global Statement

When a function must rebind an existing module-level identifier instead of shadowing it with a local copy, the global directive is required. This keyword forces the interpreter to map the specified name to the outer scope for both read and write operations.

def adjust_value(bonus):
    global score
    print("Starting total:", score + bonus)
    score = 5
    print("Modified total:", score + bonus)

score = 100
adjust_value(20)
print("Final state:", score)

Execution produces:

Starting total: 120
Modified total: 25
Final state: 5

The initial print statement accesses the global score (100) due to the global declaration. The subsequent assignment permanent overwrites the module-level variable, which is immediately reflected in the second print statement and verified by the output outside the function definition.

Tags: python variable-scope global-keyword namespace-resolution local-variables

Posted on Tue, 06 Oct 2026 16:45:12 +0000 by Termina