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

  • @python-for-everyone
    @python-for-everyone Жыл бұрын

    Checkout this video next: kzread.info/dash/bejne/ZK18vLWJisfOXdI.html

  • @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

    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.

  • @MM-oq1lb
    @MM-oq1lb7 ай бұрын

    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

    7 ай бұрын

    I appreciate that!

  • @DylanASD
    @DylanASD2 ай бұрын

    This is the clearest explanation on KZread by far. keeping up the good work from Hong Kong.

  • @python-for-everyone

    @python-for-everyone

    2 ай бұрын

    Thanks and I will!

  • @Sergio-pd3sh
    @Sergio-pd3sh2 жыл бұрын

    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!

  • @thetutorialdoctor
    @thetutorialdoctor15 күн бұрын

    I finally get it, and this will help with an issue I’m always running into. Thank you!

  • @kamranzamanni6826
    @kamranzamanni68268 ай бұрын

    Was having hard time with closures, this video explains it clearly. Thank you!

  • @python-for-everyone

    @python-for-everyone

    7 ай бұрын

    Glad it helped!

  • @piqueselio
    @piqueselio Жыл бұрын

    Very well explained! Thanks.

  • @python-for-everyone

    @python-for-everyone

    Жыл бұрын

    You're welcome!

  • @deonyz
    @deonyz10 ай бұрын

    Thank you! Third video on the subject and first now I got it.

  • @devworks8787
    @devworks8787 Жыл бұрын

    wow. never seen such an explanation. thanks a lot

  • @python-for-everyone

    @python-for-everyone

    Жыл бұрын

    You're most welcome

  • @ezracorcuera1739
    @ezracorcuera1739 Жыл бұрын

    Very helpful explanation

  • @grimkasad4183
    @grimkasad41832 жыл бұрын

    Amazing explanation!

  • @Tobi_Jones
    @Tobi_Jones Жыл бұрын

    thanks for the video, it is helpful

  • @geovaniraffaelli4508
    @geovaniraffaelli4508 Жыл бұрын

    Thank you

  • @iluvsyphonfilter
    @iluvsyphonfilter Жыл бұрын

    Fantastic explanation, thanks!

  • @python-for-everyone

    @python-for-everyone

    Жыл бұрын

    Thanks for the compliment!

  • @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!

  • @fabiopimentel6981
    @fabiopimentel6981 Жыл бұрын

    This video is pure gold

  • @python-for-everyone

    @python-for-everyone

    Жыл бұрын

    Thanks!

  • @ChrisHalden007
    @ChrisHalden0072 ай бұрын

    Great video. Thanks

  • @python-for-everyone

    @python-for-everyone

    2 ай бұрын

    You are 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: kzread.info/dash/bejne/hoh60ZaEfNCTYqw.html

  • @EW-mb1ih
    @EW-mb1ih Жыл бұрын

    great!

  • @ayophilip8997
    @ayophilip8997 Жыл бұрын

    Succinct! Thanks.

  • @anna2durai1
    @anna2durai111 ай бұрын

    💌

  • @adifirdaus8204
    @adifirdaus8204 Жыл бұрын

    nice

  • @omarozt
    @omarozt3 ай бұрын

    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

    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.

  • @johnaweiss
    @johnaweiss5 ай бұрын

    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

    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

    @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?