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 if they're in this intermediate state using closure. So, here we have a function that is creating a function.
0:09 So, create counters, creating counter the function and counter can capture and remember the variables that were ambient to it while it was created
0:19 in this intermediate state. So, it can work with start_val. It can work with increment or inc one as we called it. And as I make changes to those
0:28 they actually remember over time. Counter is a local function that remembers both inc but also the start_val actually
0:36 just changes increment though which is more impressive. Start_val and inc are captured. But notice that only inc needs nonlocal scope
0:46 right, the parameters you don't have to do that for but if you have like an intermediate to variable that's defined in the third line here
0:52 you have to say nonlocal to make that visible to this inner function.


Talk Python's Mastodon Michael Kennedy's Mastodon