Functions within functions, closures, and variable scopes in Python

preview_player
Показать описание
Functions within functions explained!

SUPPORT ME ⭐
---------------------------------------------------
Sign up on Patreon to get your donor role and early access to videos!

Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)

Want to donate crypto? Check out the rest of my supported donations on my website!

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 😄
---------------------------------------------------

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 Thanks
Рекомендации по теме
Комментарии
Автор

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.

QuantumHistorian
Автор

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...

PanduPoluan
Автор

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.)

bereck
Автор

This really clears up the confusion I was having about closures and loops

gicknardner
Автор

I seriously appreciate the pace and length of this video. It really helps a lot.

paxdriver
Автор

You are awesome at teaching complex subjects in a digestible manner.

gibbz
Автор

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!

willemvdk
Автор

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!

MatheusAugustoGames
Автор

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.

lawrencedoliveiro
Автор

This is deep! Great technicality and delivery. This is a gem.

edwardwong
Автор

Great video!
I have to rewatch the video for completely understanding this concept but it is really fascinating!

ashishjain
Автор

What about one use function:
def f():
global f
f = lambda: False
return True

also, it's fun that you can make function return itself

LechuvPL
Автор

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?

lluchi
Автор

That generator stuff at the end was well and truly cursed. Thank you

mirimjam
Автор

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.

SirGarthur
Автор

I followed until the end. That last thing breaks my head, but I'll get there once I get more experience. Though luckily I'm aware enough to know not to write this in the first place.
A double edged sword in python is really the amount of 'this thing exists within python for a reason, but that reason isn't to use it.' these things can be great for debugging, but can never be in finished code.

deldarel
Автор

Would love a video on decorators and another one on how to incorporate custom C code into python

jonasls
Автор

i once wrote a reddit post asking for useful mnemonics in python. the responses were 'durrr there are none it's all simple'... but now i have one: "VARLAC" = Values at runtime, Locations at compiletime.

prosodyspeaks
Автор

Wow good to know that WHERE value lookups are determined at compile time! That could save a bit of headaches during debugging! Excuse my English please

yxh
Автор

Happy to report that I seem to have all of this recorded in my spine already, seeing as I answered correctly to everything. I think videos like these are a good way to do some self-valuation even if you think you understand the concepts already.

I don't think I've ever used the nonlocal statement in actual code, either.

Diapolo