In Python, closures are functions that retain access to variables from their enclosing scope even after the outer function has finished executing. The nonlocal keyword allows an inner function to reference and modify variables in the outer function's scope, enabling the inner function to 'remember' and update these variables across multiple calls. This creates functions with persistent state that can be returned from the outer function and used later.
Approfondir
Prérequis
- Pas de données disponibles.
Prochaines étapes
- Pas de données disponibles.
Approfondir
Python Closures Explained: The Function That RemembersAjouté :
Normally, variables created inside functions disappear after function finishes, but there's one instance where that is not the case. In this video, you're going to learn that example.
Let's say I have a function called outer, and inside of this function, I have a variable C that has value zero.
Inside this function, I can create another function, and let's call this one inner function, and this inner function will increase the value of this C variable. But, because I want to use that C variable, I can write non-local.
This non-local keywords means look in the outer scope for a certain variable.
And now, when I write C, this will look in this outer function for this C variable. Then, I can write C plus equals one, and then I can return this C variable. After all of this is done, I will return this inner function.
Now, I will print First, before I print anything, I need to create my count variable, and this count variable will be my outer function. Now, if I call count, do you know what this will print?
If you know what this will print, do you know what this next count will print? Or the count after that? Write down in the comments below. But, this count will print one, while this count will print two, and this count will print three.
But, why? The reason is because this inner function now uses non-local C, it means it depends on this outer C. So, this inner function actually remembers the C, even though this outer function call finishes in line nine. In other words, this inner function has memory.
Such functions that have memory and can remember the variables from the past are called closures. Now, when you run the code, you see I have one, two, and three. If you found this video useful, then you must check my Python course.
And don't forget to subscribe for more useful Python videos.
Vidéos Similaires
resume fixed instantly 😭 Comment “app”andI’ll sendyou the link #parakeetaipartnership #resumetips
Ritcareer
686 views•2026-05-31
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
Making Minecraft Clone with C++ & Raylib
PecaCSLive
686 views•2026-06-04
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











