Programming Terms: Closures - How to Use Them and Why They Are Useful

Programming Terms: Closures - How to Use Them and Why They Are Useful

Corey Schafer

8 лет назад

378,593 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Ismaël Descoings
Ismaël Descoings - 28.07.2023 01:43

Where is the data stored?? This looks like quantum physics but for computers. This is quamputing

Ответить
Obj Nex
Obj Nex - 13.07.2023 18:56

Obviously intended positively and think this to be true somehow
AI's Closure - outer function (responses variable)
Corey's Closure - inner function (here it is)
:)
Thank you!

Ответить
Azeem Ahmed
Azeem Ahmed - 25.06.2023 22:24

So now we have to watch "logging" in python? 🙄

Ответить
Zoe L
Zoe L - 06.06.2023 20:17

One of my Uchicago's professor recommended this channel for learning python, until i really watched and followed along, i never know tutorials here are so invaluable. ❤Corey is a fantastic teacher in this

Ответить
Benjiizus
Benjiizus - 07.05.2023 04:34

dude this is actually crazy how easy it gets when you understand it
Thank you.

Ответить
Nasenblick
Nasenblick - 01.05.2023 11:00

Thanks for another great video that explains the mechanics, not just syntax. What I would like to understand better: What are real world use cases in which to use closures. Or in other words: In my problem solving efforts before writing code, what are patterns that call for closures.

Ответить
Don Eaton
Don Eaton - 18.03.2023 22:48

Your example doesn't work.

Ответить
Valerii Kubrak
Valerii Kubrak - 17.02.2023 16:58

print_h1('Test Headline!') = html_tag('h1')

This works under Sublime ans doesn't under VS Code.
I don't know why. The same Python version..

Ответить
berkOezer
berkOezer - 04.02.2023 23:53

The way I prefer to remember this concept:

The outer function "closes over" an inner function, therefore it's a closure. While doing so, it "injects" its local scope into the function being closed. So the function that is returned will be executed with the injected state every time it is called.

Ответить
Ratikanta Samal
Ratikanta Samal - 29.01.2023 15:01

I have recommended your channel to a lot of my colleagues over the years

Ответить
Mohie
Mohie - 15.01.2023 06:47

You are the best, I have been following your channel for years, and I always come back to your videos.

Ответить
Gresh113
Gresh113 - 13.01.2023 04:16

Mind blown

Ответить
Neo Russ
Neo Russ - 23.12.2022 19:03

Are closures the same in low level languages like c++ or rust? I never heard somebody mentioning usage of closures or callbacks in those kind of languages. Like there is something akward about it.

Ответить
Samrat
Samrat - 16.12.2022 05:10

So closures and first-class functions are basically the same thing right?

Ответить
mark cuello
mark cuello - 10.12.2022 20:20

SECURITY

Ответить
BD Sol
BD Sol - 21.11.2022 21:32

I have no words to thank you because this was a very tricky concept that I was trying to learn for last 2 days in dart and your video make me able to understand what exactly closure does, thank you so much

Ответить
kwiky
kwiky - 10.10.2022 19:13

well explained and easy to understand for beginners

Ответить
Andrew Makarskiy
Andrew Makarskiy - 10.10.2022 09:43

Wikipedia sucks, Coreyshaferpedia rocks

Ответить
Max Norman
Max Norman - 09.10.2022 18:34

Great explainer

Ответить
Srihari vaila
Srihari vaila - 02.10.2022 02:23

Thanks was very very helpful.

Ответить
Scully
Scully - 27.09.2022 13:35

So are methods that have a free variable synonymous with closures?

Ответить
Drygord Spellweaver
Drygord Spellweaver - 23.09.2022 08:15

Couldn't the wrapped functions be done just as easily by having additional parameters in a normal function? I think more complex examples would have been good to really see the utility of closure and decorated functions.

Ответить
Алексей Соков
Алексей Соков - 14.08.2022 15:31

2022

Ответить
Garden of Akira
Garden of Akira - 13.08.2022 06:39

Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.

Ответить
GHPH
GHPH - 27.07.2022 15:14

Great video :)

Ответить
Rabbani Munna
Rabbani Munna - 29.06.2022 10:23

I like the way you explain complex topics in a easy way.

Ответить
Steven Neumersky
Steven Neumersky - 21.06.2022 14:12

For all the SQL folks out there, it's like a child stored procedure that remembers its input and output and is ready to return that input and output to its parent at a moment's notice......without having to define output parameters in the child sproc.

Ответить
Jagmeet Sond
Jagmeet Sond - 04.06.2022 22:07

Top Video,really focuses on understanding.

Ответить
Nobody
Nobody - 10.03.2022 10:54

when i executed hi_func = outer_func(“hi”) , i got result hi instead of address. What is going on?

Ответить
E W
E W - 26.02.2022 04:20

I get how closures work but I don't see any real use of this concept... Maybe someone has one?

Ответить
Aparna
Aparna - 13.02.2022 15:53

super cool video, finally understood closures :)

Ответить
Velin Todorov
Velin Todorov - 08.02.2022 22:32

Is my understanding correct? - Closure remembers the variables that exist in the enclosing scope, since those variables stop to exist when the outer function is executed. Now that the variables are remembered, they can be passed to the inner function, so that the inner function can work properly. Thanks in advance

Ответить
Nick Schneider
Nick Schneider - 08.02.2022 10:18

Really great video man. Helped it click for me with deeper understanding where other videos couldn’t. Thanks!

Ответить
Dang Duang
Dang Duang - 11.01.2022 17:04

Brilliant selection of examples, you made me understand! Now I feel I am evolved!

Ответить
Chuck Tilly
Chuck Tilly - 10.01.2022 01:49

What problem do closures solve? I feel like closures add more complexity than they are worth and sacrifice code readability, troubleshooting, and maintenance.

Ответить
Danyal T
Danyal T - 17.12.2021 13:23

Awesome!

Ответить
Noa Mills
Noa Mills - 16.12.2021 20:50

Such a helpful video! Here's a heuristic I used to help understand why we need to set my_func = outer_func() in order to execute the inner function instead of just calling outer_func().

outer_func() (in the second example where we return inner_func instead of inner_func()) returns, but does not execute a function. If you want to execute it, you might want to call outer_func()() but that syntax is illegal, so we set outer_func() to the variable name my_func so we can call my_func() with just one set of parentheses. Thinking of an illegal function with double parentheses and re-writing it using legal syntax helps me conceptualize why we use my_func.

Ответить
Cosmic Odyssey
Cosmic Odyssey - 08.12.2021 06:19

Every video, got to watch another video. This is an endless while loop

Ответить
Omar K.Ali
Omar K.Ali - 06.12.2021 12:37

closure = تعني بالعربية الفناء الأمامي وفي المعنى هنا تعني فناء المتغيرات المشاع اي closure of free variables on the outer function
الغريب في هذا المفهوم هو عندما تمسك الدالة الخارجية يتم تعيين المتغير بالدالة الداخلية وهذا أمر جد غريب

Ответить
shivc22
shivc22 - 26.10.2021 15:46

What's the purpose of closures? Wouldn't it be simpler to just pass multiple variables so that the inner function isn't required? For example, you could just pass 'tag' and 'msg' as arguments instead of creating the inner function

Ответить
My Work Videos
My Work Videos - 14.10.2021 08:02

What if outer_func() has 2 inner functions inner_func1() and inner_func2() with parameters
OR
3 level nested function?s

Ответить
Abhishek Mishra
Abhishek Mishra - 11.08.2021 20:54

Guys please someone clear my doubt why variable my_func get equal to inner_func 😭

Ответить
Abhishek Mishra
Abhishek Mishra - 11.08.2021 20:49

Can someone explain how return value get equal to that variable??

Ответить
MichaelJoHarte
MichaelJoHarte - 10.07.2021 18:33

Another great video! Thanks again.

Ответить
Dina Stark
Dina Stark - 07.07.2021 15:37

Amazing Explanation bro

Ответить
Sean Adams
Sean Adams - 27.06.2021 02:49

Corey's videos are always excellent - his explanations are clear and he makes complex topics very accessible. Thank you!

Ответить