Skip to content

Closures and Captured State

Step 34 of 41

Debug Captured Values

Introduction

This is the second lesson of chapter 12 in the Comprehensions, Lambdas, Closures, and I/O section of PCAP-31-03 (22% of the score, 9 of 40 items). It covers objectives 5.3 (closures) and 5.2 (lambdas) together, through the most famous source of surprise with closures: functions created in a loop that all seem to use the last value of the loop variable. This behaviour is called late binding. It is not a bug in Python; it follows directly from the rule you learned in the previous lesson, that a closure captures a variable and not a value.