Python Scope & Memory Execution Workbench

CPython Memory Engine
Execution Code Snippet LEGB: Global
Step 0/8
Closure Late Binding Gotcha

In Python, functions created inside loops look up variables in the enclosing scope when called, NOT when created. Thus, all closures bind to the same variable `i` at its final value!

Call Stack Frames & Heap Reference Graph Cyan = Call Stack | Green = Heap Object | Purple = Closure/Function