Decorators and closures
Watch a decorator point a name at a wrapper whose cells outlive a call.
Pro lesson. Stage 1 is free to watch. Pro opens all 5 stages, 3 controls and 3 checks, in this lesson and every other.
Go Pro · $8.25/moExplain it simply
Python builds the function square, but with @, no name points to it yet. It goes straight into counted, a decorator: a function that takes a function and returns a new one. counted's frame keeps fn and count in cells, small boxes that inner functions can share. Whatever counted returns will take the name square.
The stack holds the module frame and a frame for counted. Its name fn points to cell 2304, which holds the function square. count points to cell 2384, which holds 0.
fn points to a cell, and the cell holds the function.
- 4 more stages and 3 live controls
- 2 mix-up cards and 3 predict-then-reveal checks
- Watch it as a narrated video, in three voices
- Every Pro lesson and capstone · 656 in total
$99 a year, or $15 a month. Cancel any time.