What is a CLOSURE and how to use it in Python?
After this video, you are able to describe a closure, can write one in Python and decide if it is useful for you in your code.
C H A P T E R S
0:33 The problem with changing a global variable from a function
1:47 Use a closure to encapsulate the global variable
3:00 Where the name closure comes from
3:28 Comparing closures to a class with a single function
M Y O N L I N E T R A I N I N G
🎓Like my style of teaching? Try free chapters of my online courses: pythonforeveryone.com
#pythonforeveryone #programming #python
Пікірлер: 41
Checkout this video next: kzread.info/dash/bejne/ZK18vLWJisfOXdI.html
This is the best explanation so far. Everybody is telling how to use them but no one explains WHY to use them.. So after all they are for encapsulation.
@mrklean0292
2 ай бұрын
Exactly!!! I looked at various explanations only to get the same long explanation on how they work, but not when or why you should use them.
After going through tons of tutorials of tech dudes who make videos just to make money and not to really teach newbies, this one really taught me. Thank you.
@python-for-everyone
7 ай бұрын
I appreciate that!
This is the clearest explanation on KZread by far. keeping up the good work from Hong Kong.
@python-for-everyone
2 ай бұрын
Thanks and I will!
Great stuff. That's exactly what I was after for some time now! I have been reading the online tutorials about closures, which give you same examples over and over, so that you know how it's done, but they leave you with no clue to why would you actually do it. This is the perfect explanation that i was after. Thanks a lot! I have subscribed to your channel! Keep it up, please!
@python-for-everyone
2 жыл бұрын
You are welcome! And thank you for the compliment!
I finally get it, and this will help with an issue I’m always running into. Thank you!
Was having hard time with closures, this video explains it clearly. Thank you!
@python-for-everyone
7 ай бұрын
Glad it helped!
Very well explained! Thanks.
@python-for-everyone
Жыл бұрын
You're welcome!
Thank you! Third video on the subject and first now I got it.
wow. never seen such an explanation. thanks a lot
@python-for-everyone
Жыл бұрын
You're most welcome
Very helpful explanation
Amazing explanation!
thanks for the video, it is helpful
Thank you
Fantastic explanation, thanks!
@python-for-everyone
Жыл бұрын
Thanks for the compliment!
Thank you sir, Have a great day
@python-for-everyone
Жыл бұрын
Thank you very much. I wish you a great day too!
This video is pure gold
@python-for-everyone
Жыл бұрын
Thanks!
Great video. Thanks
@python-for-everyone
2 ай бұрын
You are welcome!
I never thought about closure can actually mimic a class. Or can I say classes are just syntactic sugar for closures?
@python-for-everyone
Жыл бұрын
I also never thought about this. But there is a difference and that is that a closure typically has a single internal function. A class is more like a dictionary with data attributes and attributes with pointers to functions. I have an older video on this channel where I mimic a class with a dictionary: kzread.info/dash/bejne/hoh60ZaEfNCTYqw.html
great!
Succinct! Thanks.
💌
nice
I still don't get it. How do you call enter_num with a parameter, even it refers to a function enter_number_outer, which has no parameter?
@python-for-everyone
3 ай бұрын
I understand the confusion. enter_num_outer returns the inner function, which DOES has a parameter. So when you call enter_num_outer, enter_number_inner is called.
1:10 Why don't you need the global keyword inside the function? numbers = [] def enter_number(x): global numbers ....
@python-for-everyone
4 ай бұрын
You only need global if you want to change the value of numbers. In this case, the value (the list) is not changed, only elements in that list. I hope that answers your question.
@johnaweiss
4 ай бұрын
@@python-for-everyone Numbers? Or scalars? If you have a list of numbers, you can change a number within the list without `global`, correct?