CertGrid CertGrid
Concepts·Certified Associate Python Programmer

Python Closures

PCAP objective 5.3 is define and use closures. A closure is a nested function that keeps a reference to a variable from the function that created it - so the inner function outlives the outer call and still remembers. The mechanism is visible in `__closure__`, and the trap is late binding: a closure remembers the *variable*, not the value it had when the closure was made.

Comprehensions and Lambdas Guide 22 of 25 Advanced

Written against the versions above. `nonlocal` is Python 3 only - Python 2 could read an enclosing variable but not rebind it. Closures themselves have existed since Python 2.2.

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. A function that builds a function

  2. Where the remembered value is kept

  3. A closure that remembers between calls

  4. Without nonlocal

  5. The late-binding trap

  6. A closure as a configured function

  7. What the exam does with this objective

Official sources