Functions within functions, closures, and variable scopes in Python
62,400
Published 2022-11-21
― mCoding with James Murphy (mcoding.io/)
Source code: github.com/mCodingLLC/VideosSampleCode
SUPPORT ME ⭐
---------------------------------------------------
Sign up on Patreon to get your donor role and early access to videos!
patreon.com/mCoding
Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)
www.paypal.com/donate/?hosted_button_id=VJY5SLZ8BJ…
Want to donate crypto? Check out the rest of my supported donations on my website!
mcoding.io/donate
Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, Neel R, Matt R, Johan A, Casey G, John Martin, Mutual Information
BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Discord: discord.gg/Ye9yJtZQuN
Github: github.com/mCodingLLC/
Reddit: www.reddit.com/r/mCoding/
Facebook: www.facebook.com/james.mcoding
CHAPTERS
---------------------------------------------------
0:00 Intro
0:18 Motivating example
2:54 The One Rule
3:13 Level 1, return global
3:38 Level 2, conditional local
4:50 Level 3, nonlocal z
6:48 Level 4, cells and closures
10:35 Level 5, functions are objects, distinct closures
11:39 Level 6, donky chonky
12:52 Lambdas and comprehensions
14:32 Nonlocal and global
16:27 Level 7, please dont do this
18:18 Thank
All Comments (21)
-
Ah, your explanation on "scope determined at compile time" and "value determined at run time" really helps! Thanks! I really should start thinking of compile time / run time behaviour differences...
-
Nice video as always james, understanding how decorators and closures work can be a tad bit complex, but you explained it with crystal clear perfection. Video Request: 1.) Implementing builtin mathematical functions by yourself or creating your own complex ones efficiently. 2.) A full series on python typing, typehinting, linting and how python typecheckers like mypy, pyright work, explaining python type system and how to get good at it and not make nooby mistakes. ( Prefferably this one, because there aren't any other good guides on it on youtube, plus this fits as a idea for your channel and python typing is considered advanced level python.)
-
This really clears up the confusion I was having about closures and loops
-
You are awesome at teaching complex subjects in a digestible manner.
-
This is deep! Great technicality and delivery. This is a gem.
-
I seriously appreciate the pace and length of this video. It really helps a lot.
-
Great video! I have to rewatch the video for completely understanding this concept but it is really fascinating!
-
James, you keep surprising me with very interesting stuff! Love it. I've been programming Python for years and I really thought I understood variable scope etc. But this is a new insight to me and actually really helpful!
-
I was litterally sitting in pycharm yesterday trying out various configurations of some testcode i wrote JUST to better understand this concept. Crazy good timing.
-
That last example was masterful! I can't wait to obfuscate all code in production to be the only one that understands it, and keep my job!
-
Wow, it took me quite a while to fully grasp some of the examples. Thanks for the video. This is very specific, but are you planning a video on Python wheels and possibly how to build them for different platforms, such as with Github actions?
-
14:42 “global” and “nonlocal” are actual declarations in Python. “def” and “class” are not declarations, since they construct function and class objects every time they are executed.
-
Long story short. Don't do any of that crazy stuff in your python code. If you're nesting function definitions within function definitions and reusing global scope names at every stage, refactor your code back to sanity.
-
I always look forward to your videos. Thanks!
-
Bro you got the BEST examples and demonstrations!
-
Crystal clear, very good video
-
Your channel is like heaven
-
You're really good at explaining!
-
Great video, have a exam next week on closures and static/dynamic scope.
-
Awesome, content, as always!