What is a CLOSURE and how to use it in Python?

  Рет қаралды 13,385

Python for Everyone

Python for Everyone

Күн бұрын

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: pythonforevery...
#pythonforeveryone #programming #python

Пікірлер: 44
@python-for-everyone
@python-for-everyone Жыл бұрын
Checkout this video next: kzbin.info/www/bejne/aKqri4aGj8mbY9E
@MM-oq1lb
@MM-oq1lb 11 ай бұрын
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
@python-for-everyone 11 ай бұрын
I appreciate that!
@swordlion294
@swordlion294 Жыл бұрын
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
@mrklean0292 6 ай бұрын
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.
@Sergio-pd3sh
@Sergio-pd3sh 2 жыл бұрын
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
@python-for-everyone 2 жыл бұрын
You are welcome! And thank you for the compliment!
@piqueselio
@piqueselio 2 жыл бұрын
Very well explained! Thanks.
@python-for-everyone
@python-for-everyone 2 жыл бұрын
You're welcome!
@iwatchtvwithportal5367
@iwatchtvwithportal5367 Жыл бұрын
I never thought about closure can actually mimic a class. Or can I say classes are just syntactic sugar for closures?
@python-for-everyone
@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: kzbin.info/www/bejne/ioWpoGeBgdJgaKs
@ezracorcuera1739
@ezracorcuera1739 Жыл бұрын
Very helpful explanation
@deonyz
@deonyz Жыл бұрын
Thank you! Third video on the subject and first now I got it.
@fabiopimentel6981
@fabiopimentel6981 Жыл бұрын
This video is pure gold
@python-for-everyone
@python-for-everyone Жыл бұрын
Thanks!
@JackMark-rm6cu
@JackMark-rm6cu Жыл бұрын
Thank you sir, Have a great day
@python-for-everyone
@python-for-everyone Жыл бұрын
Thank you very much. I wish you a great day too!
@iluvsyphonfilter
@iluvsyphonfilter Жыл бұрын
Fantastic explanation, thanks!
@python-for-everyone
@python-for-everyone Жыл бұрын
Thanks for the compliment!
@geovaniraffaelli4508
@geovaniraffaelli4508 Жыл бұрын
Thank you
@ayophilip8997
@ayophilip8997 Жыл бұрын
Succinct! Thanks.
@DylanASD
@DylanASD 5 ай бұрын
This is the clearest explanation on KZbin by far. keeping up the good work from Hong Kong.
@python-for-everyone
@python-for-everyone 5 ай бұрын
Thanks and I will!
@anna2durai1
@anna2durai1 Жыл бұрын
💌
@grimkasad4183
@grimkasad4183 2 жыл бұрын
Amazing explanation!
@thetutorialdoctor
@thetutorialdoctor 4 ай бұрын
I finally get it, and this will help with an issue I’m always running into. Thank you!
@devworks8787
@devworks8787 Жыл бұрын
wow. never seen such an explanation. thanks a lot
@python-for-everyone
@python-for-everyone Жыл бұрын
You're most welcome
@Tobi_Jones
@Tobi_Jones Жыл бұрын
thanks for the video, it is helpful
@azizbekibnhamid642
@azizbekibnhamid642 2 ай бұрын
Thanks bro, just as simple explanation as python
@kamranzamanni6826
@kamranzamanni6826 11 ай бұрын
Was having hard time with closures, this video explains it clearly. Thank you!
@python-for-everyone
@python-for-everyone 11 ай бұрын
Glad it helped!
@omarozt
@omarozt 7 ай бұрын
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
@python-for-everyone 7 ай бұрын
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.
@VURITISAIPRANAYCSE2021VelTechC
@VURITISAIPRANAYCSE2021VelTechC Ай бұрын
definitely the best and short explanation i found so far, good work bro
@python-for-everyone
@python-for-everyone Ай бұрын
Well thank you very much!
@johnaweiss
@johnaweiss 8 ай бұрын
1:10 Why don't you need the global keyword inside the function? numbers = [] def enter_number(x): global numbers ....
@python-for-everyone
@python-for-everyone 8 ай бұрын
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
@johnaweiss 8 ай бұрын
@@python-for-everyone Numbers? Or scalars? If you have a list of numbers, you can change a number within the list without `global`, correct?
@ChrisHalden007
@ChrisHalden007 6 ай бұрын
Great video. Thanks
@python-for-everyone
@python-for-everyone 6 ай бұрын
You are welcome!
@EW-mb1ih
@EW-mb1ih Жыл бұрын
great!
@adifirdaus8204
@adifirdaus8204 2 жыл бұрын
nice
Programming Terms: Closures - How to Use Them and Why They Are Useful
11:44
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 443 М.
БЕЛКА СЬЕЛА КОТЕНКА?#cat
00:13
Лайки Like
Рет қаралды 2,4 МЛН
The Joker wanted to stand at the front, but unexpectedly was beaten up by Officer Rabbit
00:12
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 9 МЛН
Python Closures for Beginners | Python tutorial
19:32
Dave Gray
Рет қаралды 8 М.
Naming Things in Code
7:25
CodeAesthetic
Рет қаралды 2,1 МЛН
What is Scope in Python??
10:25
b001
Рет қаралды 50 М.
Python Decorators: The Secret to Supercharging Your Code
20:53
You Can Do Really Cool Things With Functions In Python
19:47
ArjanCodes
Рет қаралды 222 М.
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 274 М.
Closures in Python | Explained with animations
8:08
Sreekanth
Рет қаралды 24 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
The Absolute Best Intro to Monads For Software Engineers
15:12
Studying With Alex
Рет қаралды 634 М.