CertGrid CertGrid
Concepts·Certified Entry-Level Python Programmer

Python Scope, Shadowing and global

The environment half of objective 4.2. Python resolves a name through four scopes - local, enclosing, global, built-in - and decides whether a name is local by looking for an assignment *anywhere* in the function, including below the line that reads it. That single rule produces `UnboundLocalError`, which is the most confusing error in the objective and completely predictable once you know what causes it.

Functions and Scope Guide 20 of 26 Intermediate

Written against the versions above. `nonlocal` arrived in Python 3.0 and is the one keyword here that Python 2 does not have. The `UnboundLocalError` message was reworded in Python 3.11 to name the variable; the behaviour is unchanged.

One machine, and any shell with Python 3 will do - these exams test the language, not a distribution.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSPython 3.14.4 - the only machine this path needs2 Core4 GB50 GB

Before you start

  1. Reading a global, and shadowing one

  2. The read that fails because of a line below it

  3. global: assign to the module-level name

  4. nonlocal: assign to the enclosing function's name

  5. The four scopes, innermost first

  6. A local exists only while the call does

  7. What the exam does with this objective

Official sources