Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Concept: Function closures

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Python functions capture data
0:02 if they're in this intermediate state using closure.
0:05 So, here we have a function that is creating a function.
0:08 So, create counters, creating counter the function
0:11 and counter can capture and remember the variables
0:15 that were ambient to it while it was created
0:18 in this intermediate state.
0:19 So, it can work with start_val.
0:22 It can work with increment or inc one as we called it.
0:25 And as I make changes to those
0:27 they actually remember over time.
0:30 Counter is a local function that remembers
0:32 both inc but also the start_val actually
0:35 just changes increment though which is more impressive.
0:38 Start_val and inc are captured.
0:41 But notice that only inc needs nonlocal scope
0:45 right, the parameters you don't have to do that for
0:47 but if you have like an intermediate to variable
0:49 that's defined in the third line here
0:51 you have to say nonlocal to make
0:52 that visible to this inner function.