Python Memory Management and Tips Transcripts
Chapter: Memory and functions
Lecture: Concept: Closure state
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's round out this chapter on functions by looking at closures and closure state. So we saw that we could almost created a little object,
0:08
a little class that has some behavior and has some data by using closures. So our "create_counter" function here,
0:15
take some arguments to get things started. It's going to create a variable called "current", and it's going to capture that using "nonlocal".
0:24
It's also going to capture "step" by the way, we just don't have to say nonlocal for it.
0:29
So this counter implementation that gets returned will have a permanent reference to current and a
0:35
permanent reference to step. This is like creating a class that has one function, which is whatever this does here,
0:42
do the step, and then two fields, current and step. And when we create one, here
0:47
this one, we're starting a 10 and incriminating by 7, so we're calling it "by_7s".
0:51
When we call this function, first time it used it's initial variables that it's held onto, but then the next time, it's remembering
0:58
Its state. It's holding current and step and putting them together, and again, and again, and
1:04
again. So when you create a closure like this, just keep in mind that you're changing the life cycle of current and step.