"Therefore, in simple terms: A closure is an inner function that remembers and has access to variables in the local scope in which it was created even after the outer function has finished executing"... It is absolutely impossible for this guy not to make heaven
@user-zo2ky4mz7d4 жыл бұрын
He is a fantastic teacher no doubt 👌🏽
@souranumaji42134 жыл бұрын
No one knows how to teach a complicated topic better than him
@treyheaney94834 жыл бұрын
no it's not
@cegonaove3 жыл бұрын
You got it fam
@nirajraut94083 жыл бұрын
@@treyheaney9483 Why? Could you please explain?
@kickbuttowsk2i4 жыл бұрын
okay, let's watch the decorators video at last. You rock, Corey.
@satoshinakamoto1714 жыл бұрын
i dont think people say this enough, but your channel is probably one of the most valuable programming tutorial channels in YT. i keep learning from this channel and i all i can give is my appreciation. thank you.
@dineshraj78 Жыл бұрын
true, i felt the same! great help!
@varunkumar_vk5 жыл бұрын
"My hope here is that, by seeing this in two different languages, it will really knock home the concept, rather than just the syntax of the language". Absolute brilliance!
@ossenaar7 жыл бұрын
Best (Python) teacher who explain concepts easily and clear. As a beginner programmer, I read and study Python concepts in books and jupyter notebook samples. But, for certain concepts, I need additional visual explanations and Corey's videos always help me out greatly! In eBooks, I always highlight important concepts with a note link to his KZbin video to revisit the videos later.
@mijaelwatts6 жыл бұрын
Man, I tried to learn Closures last year and failed miserably. Today I gave it another try(this time with ur video) and now I totally get it. Thank you very much man. God Bless!!!
@supermariosunshine647 жыл бұрын
So, if I got it right, closures are when a function remembers the environment it was created it, specifically the variables around it.
@coreyms7 жыл бұрын
Yes, that is a good concise description.
@samiullah91543 жыл бұрын
Thanks you just nailed it 😇
@samiullah91543 жыл бұрын
You nailed it almost 3 years ago😂
@hemanth69513 жыл бұрын
Looks like normal human childrens behaviour to remember which city 🏙️ born
@NKATIKADALAVIDYASRI3 жыл бұрын
Some clarify me with another example - So that I will get it 100% please....
@Max-my6rk7 жыл бұрын
Thanks very much. This is the only video explaining Closures the way it should be.... very clear and easy to understand! U r truly awesome!
@scuti7073 Жыл бұрын
free variable is the variable that is not defined within the inner function but is defined in the enclosing function. It can still be accessed in the enclosed function. "A closure is an inner function that remembers and have memory of the variables from the local scope from which it was created created (this includes arguments passed to the parent function)- even after the outer function has finished executing"- 4:50 A closure closes over the free variables from their environment- defined outside of the function/
@souranumaji42134 жыл бұрын
Video on "First Class Functions" is the base. Once you clear that idea, understanding closure becomes much easier... Corey you are genuine and genius teacher, a teacher everyone deserves to have at least once in their life.
@santoshkc19225 жыл бұрын
You are the only person who can make me understand Python!!! Your excellent methodology of making the concepts clear and those top notch examples gives a break through to any person willing to learn the language. Thank you a lott!! God bless you
@Benjiizus Жыл бұрын
dude this is actually crazy how easy it gets when you understand it Thank you.
@DanCanning5 жыл бұрын
Never mind first class functions. This was a first class explanation. You closed off my confusion nicely. :)
@rabbanimunna69922 жыл бұрын
I like the way you explain complex topics in a easy way.
@bhaktitarang044 жыл бұрын
Wow man, the teaching skills that you possess is unmatchable
@BusinessDevelopmentSolution2 жыл бұрын
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
@oregonbry7 жыл бұрын
Thank you! After all these years, I feel like I am finally starting to understand Closures. Awesome!
@nicholaszustak7687 жыл бұрын
Completely forgot these from my classes; this video helped me remember and learn it way more in depth! Thank you
@ratonmagico6 жыл бұрын
When I'm coding in Python and come across with something I don't know or I don't remember quite well I come back to your videos. So clear and insightful at the same time!
@RalphAlberg3 жыл бұрын
Corey's videos are always excellent - his explanations are clear and he makes complex topics very accessible. Thank you!
@iftrejom3 жыл бұрын
I spent the entire day trying to figure out what closure and failed miserably, and then this guys clears it up in 5 minutes. Great job, man, thank you a lot.
@gauthamambethkar44834 жыл бұрын
Thanks Corey. Nobody can explain this the way you do.
@amanmaldar6 жыл бұрын
4:36 That's what a close it. I am yet to complete watching video. But just the way you put forward these concepts, always keeps me going through your videos. Thank you sir.
@ratikantasamal3201 Жыл бұрын
I have recommended your channel to a lot of my colleagues over the years
@gatsby3354 жыл бұрын
I am here from the *Decorators!*
@prathyushaguduru86223 жыл бұрын
lol! me too 😂
@melodyparker34853 жыл бұрын
Me too!
@bErKcorner Жыл бұрын
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.
@josefh87824 жыл бұрын
Another great video. Though a complicated topic. I understand how they work from your examples, but I'd like to know more about when and why we would need to use these. It's one thing to understand how these things work technically, but its another leap in understanding to know when and why to use a certain approach to solve a problem.
@tede18384 жыл бұрын
after a few KZbin vids and reading a couple of Python books, i finally found someone who explains Closures and Decorators to a level that helps me to completely understand them and their uses. thanks for your efforts Corey
@Trippykiyay Жыл бұрын
You are the best, I have been following your channel for years, and I always come back to your videos.
@dinastark10883 жыл бұрын
Amazing Explanation bro
@IrshadKhan-vk8rn4 жыл бұрын
All My interview for The MNC interviewer ask what is closure i explained all as i remembers and finally i am all in previous year. Even Interviewer Knows About You Corey and we discuss how awesome your videos are ...:-) Currently working there have been 1 year and in core development team thanks corey again.
@JG-mw7yd4 жыл бұрын
Hey Corey, recently stumbled on your videos and I'm so happy I did, thanks for taking the time to make such great tutorial videos! Also, for those with slower auditory processing like me, set the playback to 0.75 speed, it is perfect!
@台灣運動聊天室6 жыл бұрын
Those are 3 great examples. And I like your explanation where you say that a closure encloses the outer free variables inside the inner function (paraphrasing).
@DracoWF7 жыл бұрын
Man, thank you! Googled everywhere with no luck and finally found you and understood closures ;)
@Indraw7055 жыл бұрын
I really like the way you represent all those things in very easy manner
@rrichc4 жыл бұрын
Thank you for this explanation! Great approach in making the video language agnostic. I came across this topic in studying Dart with Flutter where closures and anonymous functions are used frequently and this made things clear.
@noamills34582 жыл бұрын
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.
@shady4tv5 жыл бұрын
The amount of function passing in the last example was nuts haha. it took me writing the code out and writing my own comments plus reading out loud to fully follow the logic here. so if I understand it correctly - add_logger = logger(add) takes the add function (NOT EXECUTED) as a parameter to the logger function (which also just passed the function without executing it). Since the logger function was called using the brackets "()" it was executed and returned the inner function (log_func) because of the return log_func at the end of the logger function. But because of closures it remembered that you passed in the add function. So when you executed add_logger(3, 3) it passed the add function into the log_func and executed the add function with the supplied arguments within the print function. just wow - that is a brain twister.
@nickschneider1012 жыл бұрын
Really great video man. Helped it click for me with deeper understanding where other videos couldn’t. Thanks!
@Wes-Tyler4 жыл бұрын
Extremely well-explained tutorial, but I still can't figure out why I would ever want to use this..
@pipertripp4 жыл бұрын
His last example with the logging is a good example, though as he says, decorator would be the more pythonic way (although it's still using a closure as well). The way that he's using the logging wrapped around the other two functions, add and subtract, show how you can apply logging to any arbitrary function in your application without having to replicate all of the logging machinery in every function. It makes your code way more compact and also far more maintainable.
@tongliu10766 жыл бұрын
Wow. Couldn't understand better than after watching this great video!
@prmurali1leo4 жыл бұрын
no one can explain like him
@saimanojnandam32117 жыл бұрын
You are the best teacher I've seen
@link65632 жыл бұрын
Every video, got to watch another video. This is an endless while loop
@pumpkinking7158 жыл бұрын
Great video! I really liked the examples you used. They really helped to demonstrate actual use cases for closures. I am looking forward to the python decorator tutorial
@simonchung98137 жыл бұрын
Excellent video, Corey. Explanation is concise and the code snippets effectively demonstrate the the concepts of closures.
@haos45742 жыл бұрын
Brilliant selection of examples, you made me understand! Now I feel I am evolved!
@d_learner75736 жыл бұрын
Another great video. This was one of the hardest subjects for me and I understood it very well. Thank you Corey.
@nasenblick Жыл бұрын
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.
@kwiky56432 жыл бұрын
well explained and easy to understand for beginners
@akira_asahi2 жыл бұрын
Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.
@BanibrataDutta3 жыл бұрын
Amazing explanation. Having read and seen other explanations, can vouch for this one as being the one that I found understandable.
@GrubenM5 жыл бұрын
Very helpful concretization of a pretty abstract term, thanks!
@bulldawg44984 жыл бұрын
Good job! Simple and precise ... Thanks!
@ermaolaoye4 жыл бұрын
Thank you for making these wonderful videos! They're really helpful when I tried to understand these concepts!
@Qornv8 жыл бұрын
Your explanations and video quality are top notch
@alejandronieto5767 ай бұрын
Great video. Thanks a lot from Argentina.
@donepudiaditya66436 жыл бұрын
Awesome tutorials. The greatest Python teacher i must say
It's similar to nested looping to me. outer function(arg) ~ item i of list A, then inner function(arg) ~ item j of list B. Gives you flexibility/control over the context of the inner function.
@timmyl93615 ай бұрын
Thnx dude. This is all I needed to understand it
@marcc11794 жыл бұрын
Thank you! You are a great teacher!
@objnex Жыл бұрын
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!
@omnesomnibus28454 жыл бұрын
Imagine yourself playing a video game where you can get a certain gun, but it can have different kinds of ammunition. Heat seeking, explosive, dimension opening, whatever. In this case, the inner function is like the gun, and the variables that are put into it when the outer function is called is like the ammunition. You are calling for a specific gun already loaded with some kind of ammunition. Or, maybe a better way of putting it is that the outer function adds mods to the base gun, like a specific kind of stock, a sight, a longer magazine, etc. Then when you call the outer function it gives you this modified gun. When you now use that, you put in the ammo, which is the arguments given to the inner function.
@marflage4 жыл бұрын
That was actually a pretty good way to put it. Thanks!
@jagmeetsond60752 жыл бұрын
Top Video,really focuses on understanding.
@ganeshjayaraman25887 жыл бұрын
Superb Video sir .. Thanks for explaining the wiki's definition with an example .. Really helped me :)
@alinandrei65553 жыл бұрын
Would've liked to see a more technical explanation of closures, including more concepts like nonlocal, the @wraps as an intro probably. Thank you!
@hendroyohanes42957 жыл бұрын
I came here because you told me in the Decorators tutorial to understand closures first
@ForWork-mj9fvАй бұрын
Thank you Corey
@aparna14982 жыл бұрын
super cool video, finally understood closures :)
@DivyanshuBansal6 жыл бұрын
Hi Corey, I really like your videos. You explain everything in a very elaborated manner and that too with practical examples which helps in getting deeper insights and developing intuition.Keep the good work up. Also, Please upload more videos of Bronx, haven't seen his new videos.PS-> his expression in running late video still makes me smile( I've watched it multiple times). :-) :-)
@coreyms6 жыл бұрын
Haha, will do!
@mrmatt89986 жыл бұрын
learning python as javascript developer . it seems python and javascript similar in some ways . python is really nice language :) . to be honest more cleaner than javascript.
@prashantpant28767 жыл бұрын
"A closer closes over a free variable from its environment"
@rayrayrun4 жыл бұрын
its free real estate
@WasiMaster4 жыл бұрын
6:22
@badr_mo8 жыл бұрын
My Savior...Thaaanks so much for this simplicity...
@TrueHeresy4 жыл бұрын
This video made it click, thank you!
@vishnupandey3390Ай бұрын
# A closure closes over the free variable of the environment def fun1(num): def getit(): print(num) return getit closure = fun1(2) closure()
@premkumark71574 жыл бұрын
Hi Mr. Corey, Please make videos for data structures and algorithms using Python. Its hard finding tutorials for DSA in python. I believe i can understand the concepts easily if you teach the DSA.
@tcarney577 жыл бұрын
Thanks for the video. I've watched it a few times, and I'll no doubt watch it several more times before I get it. I'm beginning to see how closures work, but I'm completely in the dark about why I'd ever want to use them. What do closures allow me to do that I can't do with simple procedures or ordinary functions? Putting a string between html tags is pretty simple with just an ordinary function, with the string to be tagged passed in as a plain-old parameter. Why would I want to add a layer of confusion by using a closure instead?
@coreyms7 жыл бұрын
Hey there. That's a great question. I program mainly in Python and I can give you a few examples of where closures make a huge difference in that language compared to normal functions. First, closures can be used for logging or measuring performance. So instead of specifying putting your logging or performance code before and after each and every call to the function, you could instead just wrap the function once and get that functionality everywhere. Closures are also used to add functionality to functions across the board. For example, for web frameworks such as Flask, you can decorate your functions with routing closures that perform all of the lower level web stuff in the background and allow you to simply focus on what you return to the client. There are many many more examples, but those are a couple off the top of my head.
@AsavPatel8 жыл бұрын
one of the best videos on closures. CLEAN and CONSICE. keep up the good work +Corey Schafer (y) I have one request, can you do a video on "Variable bindings (early vs late binding) in python" Thanks!
@subhasish226 жыл бұрын
Really nice video. Very well explained.
@jcnarasimhan42184 жыл бұрын
Read thru' comments...Added my dumbo notes based on my understanding, pardon the unruly-looking english: ''' The returned function namely, log_func sits into a variable from the calling side...like add_logger The key...light bulb...here is that the variable func is frozen in the context/closure when the calling side makes the call it's frozen with the value = "add" so, the following is in the memory stack / context when you call with "add_logger=logger(add)" func=add And log_func is returned into add_logger with the above context ... so: add_logger = log_func with func= add defined in the scope for add_logger context/closure I.O.W: add_logger=log_func is tied/locked with the variable func=add The next time you call add_logger(*args), you are actually calling log_func with func=add...so: def log_func(*args): logging.info('Running "{}" with arguments {}'.format(func.__name__,args)) print(func(*args)) will call via: func(*args) add(*args coming from the call of add_logger(*args))... ########### see the foll. scrap code : def log_func(func,*args): print(func(*args)) def add(x,y): return x+y def sub(x,y): return x-y log_func(add,3,3) log_func(sub,3,3) '''
@maltebp6 жыл бұрын
Brilliant! It finally starts to make sense, what closures actually are :) Thank you, for a very pleasant and informative video.
@홍성의-i2y10 ай бұрын
4:24 definition of closure 6:17: a fancier way to define closure
@jixuancheng7 жыл бұрын
I really like this example...thank you very much
@DivyYadav-hg2js17 күн бұрын
so basically i understood closer function its a function that will work only if we execute the inner func using a variable or assigning a variable to the outer func to disclose the inner func message only when we execute it . am i right corey?
@mashraful50654 жыл бұрын
Daaaaaaaaaaaaaamn man Corey is a beast
@Lahiru_Udana5 жыл бұрын
Thanks for your effort
@woody50125 жыл бұрын
Great video, well explained with good examples. :)
@chronicfantastic8 жыл бұрын
Your videos are fantastic, thanks a lot.
@jhgfdjhgfdhdjfjhd67215 жыл бұрын
Thanks so much for your efforts
@Dj.julien.b5 жыл бұрын
Very useful ! I love your videos I learn so fast! :) thank you for making this golden content you provide :)
@thapasujan072 ай бұрын
Thank you, Sir.
@MichaelJoHarte3 жыл бұрын
Another great video! Thanks again.
@onzamono2 жыл бұрын
4:21 Me: What kind of sorcery is this!?
@leonardogrinstein98156 жыл бұрын
Hey Corey. I found out something very interesting that is more efficient and more readable than what you explained. If you want to run an inner function you could just add another parenthesis, like this: outter_func()(). So, in your last example, instead of writing: add_logger = logger(add) and add_logger(3, 3), you could just write: logger(add)(3,3). Don't you think this is much easier and readable?
@velintodorov28342 жыл бұрын
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
@sriharivaila72762 жыл бұрын
Thanks was very very helpful.
@rakeshraushan65733 жыл бұрын
"And that my friend is what they call closure" #FriendsFan #Rachel