Career upgrade: Learn practical AI skills for better jobs and higher pay.
Level up

Scope, Shadowing, global, and Local Errors

Key Takeaways

  • A name assigned inside a function is local to that function unless declared global.
  • Local variables disappear after the function call returns, but returned values can be stored by the caller.
  • A local name can shadow a global name without changing the global object.
  • Reading a missing name raises NameError, while reading a local name before assignment can raise UnboundLocalError.
  • The global statement lets a function rebind a module-level name, but PCEP snippets usually use it to test tracing rather than design style.
Last updated: May 2026

Names live in scopes

A scope is the region of a program where a name can be found. PCEP mainly tests module-level names and function-local names. The module level is often called global scope. Each function call creates a new local scope for that call.

x = 10

def show():
    x = 3
    print(x)

show()
print(x)

This prints 3 and then 10. The assignment inside show creates a local x; it does not change the global x.

Shadowing

Shadowing happens when an inner scope uses the same name as an outer scope. Shadowing is legal, but it can make exam snippets harder to read because the same spelling points to different variables at different lines.

Location of assignmentName categoryVisible where?
Outside functionsGlobalModule and readable inside functions
Inside a functionLocalThat function call only
Inside function with global xGlobal rebindingModule-level x

A function can read a global name if it does not assign that same name locally.

rate = 2

def cost(n):
    return n * rate

print(cost(5))

The function reads the global rate, so the result is 10.

The local assignment trap

If a function assigns to a name anywhere in its body, Python treats that name as local throughout the function unless a global declaration says otherwise. This matters even if the read appears before the assignment.

count = 1

def bump():
    print(count)
    count = count + 1

bump()

This does not print the global value. Because count is assigned in the function, Python treats it as local. The first line tries to read local count before it has a value, so Python raises UnboundLocalError, a more specific kind of NameError.

NameError and global

A plain NameError occurs when Python cannot find a requested name in the relevant scopes.

def report():
    print(total)

report()

If no global total exists, this raises NameError. To rebind a global name from inside a function, use global, but do so intentionally.

count = 1

def bump():
    global count
    count = count + 1
    return count

print(bump())
print(count)

Both prints show 2, because the function rebounded the module-level count.

Scope and returned values

A local variable can outlive the function only if its value is returned or stored in a mutable object outside the function. For PCEP, focus on return values.

def make_score():
    score = 95
    return score

result = make_score()
print(result)

The local name score disappears after the call, but the integer value it returned is stored in result. Trying to print score at the module level after this call would raise NameError, because the name was local to the function and was never created globally.

Exam approach

When a snippet uses repeated names, annotate each assignment:

  • Mark global assignments before any def statements.
  • Inside each function, mark local assignments.
  • If global name appears, route assignments to the module-level name.
  • Check whether a name is read before it exists.
  • Remember that each function call gets a fresh local scope.

Most scope mistakes on PCEP come from reading the code visually instead of following Python's name-resolution rules.

Test Your Knowledge

What does shadowing mean in a basic Python function question?

A
B
C
D
Test Your Knowledge

Which exception is most appropriate when code tries to print a name that has never been defined in any accessible scope?

A
B
C
D
Test Your Knowledge

What is the effect of global count inside a function?

A
B
C
D