Functions within functions, closures, and variable scopes in Python

  Рет қаралды 61,611

mCoding

mCoding

Күн бұрын

Functions within functions explained!
― mCoding with James Murphy (mcoding.io)
Source code: github.com/mCodingLLC/VideosS...
SUPPORT ME ⭐
---------------------------------------------------
Sign up on Patreon to get your donor role and early access to videos!
/ mcoding
Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)
www.paypal.com/donate/?hosted...
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
Github: github.com/mCodingLLC/
Reddit: / mcoding
Facebook: / 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 Thanks

Пікірлер: 153
@PanduPoluan
@PanduPoluan Жыл бұрын
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...
@paxdriver
@paxdriver 19 күн бұрын
I seriously appreciate the pace and length of this video. It really helps a lot.
@bereck7735
@bereck7735 Жыл бұрын
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.)
@trag1czny
@trag1czny Жыл бұрын
ball
@bereck7735
@bereck7735 Жыл бұрын
@@trag1czny ball
@eta3323
@eta3323 Жыл бұрын
I'd love to see a series on typing too.
@112BALAGE112
@112BALAGE112 Жыл бұрын
Your first question is answered on stackoverflow /q/2284860
@gicknardner
@gicknardner Жыл бұрын
This really clears up the confusion I was having about closures and loops
@gibbz00
@gibbz00 Жыл бұрын
You are awesome at teaching complex subjects in a digestible manner.
@edwardwong654
@edwardwong654 Жыл бұрын
This is deep! Great technicality and delivery. This is a gem.
@ashishjain518
@ashishjain518 Жыл бұрын
Great video! I have to rewatch the video for completely understanding this concept but it is really fascinating!
@l4luchi123
@l4luchi123 Жыл бұрын
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?
@willemvdk4886
@willemvdk4886 Жыл бұрын
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!
@GOTHICforLIFE1
@GOTHICforLIFE1 Жыл бұрын
I think the reason you DON'T know about this though, is that if you encounter anything like this in real life i doubt the immediate reaction would be "hmm, interesting". Likely closer to "Hmmm, this needs to be fixed" 😅 But for the sake of demonstration, it certainly is cool to see how the inner workings actually pan out
@QuantumHistorian
@QuantumHistorian Жыл бұрын
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.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
Fun fact: even noddy old PHP has added “use”-clauses so it can do this kind of thing. Maybe the concept is more useful than you realize?
@QuantumHistorian
@QuantumHistorian Жыл бұрын
​@@lawrencedoliveiro9104 Maybe. Can you give me an example where nested functions that shadow variable scope is better than linearising your functions and having distinct variable names? I don't think I've ever seen any, and I struggle to dream one up. Maybe in something like decorators? But even then messing around with scope seems unnecessary...
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
@@QuantumHistorian Shadowing is incidental, rather than being something you deliberately do. But lexical binding is certainly extremely useful in conjunction with functions as first-class objects. If you look up my HarfPy binding for HarfBuzz, for example, the way I define the wrappers for defining callbacks makes heavy use of closures. I estimated that doing it this way saved me about 200 lines of repetitive code.
@QuantumHistorian
@QuantumHistorian Жыл бұрын
@@lawrencedoliveiro9104 Thanks for actually giving an example, I'll look into it!
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
@@QuantumHistorian That same module also includes a def_struct_class function, which creates, not just a function closure, but a whole class closure.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
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.
@SirGarthur
@SirGarthur Жыл бұрын
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.
@MatheusAugustoGames
@MatheusAugustoGames 11 ай бұрын
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!
@jonasls
@jonasls Жыл бұрын
Great video, have a exam next week on closures and static/dynamic scope.
@rossli8621
@rossli8621 7 ай бұрын
Bro you got the BEST examples and demonstrations!
@dane2565
@dane2565 Жыл бұрын
I always look forward to your videos. Thanks!
@royler8848
@royler8848 Жыл бұрын
Crystal clear, very good video
@etis398
@etis398 Жыл бұрын
You're really good at explaining!
@prashlovessamosa
@prashlovessamosa Жыл бұрын
Your channel is like heaven
@deldarel
@deldarel Жыл бұрын
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.
@__python__
@__python__ Жыл бұрын
Thanks James for this video which helps to assimilate the subtleties of nested functions. Can you make a video on the most important design patterns? thank you in advance.
@yxh
@yxh Жыл бұрын
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
@Phaust94
@Phaust94 Жыл бұрын
Awesome, content, as always!
@mirimjam
@mirimjam Жыл бұрын
That generator stuff at the end was well and truly cursed. Thank you
@AIRLZ6
@AIRLZ6 2 ай бұрын
Great Video! to the point👌
@jonasls
@jonasls Жыл бұрын
Would love a video on decorators and another one on how to incorporate custom C code into python
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
As extension modules or via ctypes?
@jonasls
@jonasls Жыл бұрын
@@lawrencedoliveiro9104 I'm really not sure, I think that's something that could be addressed. Some type of overview maybe.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
Extension modules are where you write special C code to wrap the functionality of other C code as Python objects. ctypes is a standard Python module which lets you write the wrapper in pure Python. This usually makes for much less code than writing extension modules.
@Diapolo10
@Diapolo10 Жыл бұрын
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.
@Mutual_Information
@Mutual_Information Жыл бұрын
My guesses: ('chonky x', 'y arg', 'donky z').. I thought it was either.. most inner scopes (my guess) or most outer scopes ('global x', 'y arg', 'outer z'). Lol yea real surprised it was a mix of the two.. Good link we got that rule. Also didn't realize closure was an attribute of the function (but ah, of course it has to be)! Knowing that's available to inspect would have helped my closure confusion in the past.
@LechuvPL
@LechuvPL Жыл бұрын
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
@mCoding
@mCoding Жыл бұрын
This is both wonderful and horrifying.
@zalzalahbuttsaab
@zalzalahbuttsaab Жыл бұрын
As a newbie I understand that for a function to run, it must be called by name. None of the functions shown in the initial example are externally called. If the first (outer) function is called, that function does not call the functions nested within it, so from my understanding, those functions would just sit there and do nothing, i.e., not operate.
@higorhi72
@higorhi72 Жыл бұрын
Functions are first-class objects, same as any other variable. You can define a function called F, then bind it to G, and use it through G as you normally would with F. Try creating a function that takes a function as an argument and runs it on the other passed arguments - you'll get the hang of it easily.
@jma42
@jma42 Жыл бұрын
I wish I learned closures from you, this programming concept is frikkin hell and is mostly the cause of "magic" not only in python but in every other different languages as well
@prosodyspeaks4036
@prosodyspeaks4036 8 ай бұрын
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.
@stevenhe3462
@stevenhe3462 Жыл бұрын
Scheme suddenly looks good to me with its sane lexical scoping, and JavaScript. And then I remembered that JavaScript was heavily inspired by Scheme.
@nHans
@nHans Жыл бұрын
Some other languages I've programmed in ( **cough** Java **cough** ) would complain that *a = None* following *if False:* is unreachable. So Python doesn't complain about such things? What about Python linters / style checkers?
@mCoding
@mCoding Жыл бұрын
The compiler doesnt care, but I did have to turn my linter off for the video so it didn't highlight that section.
@Mnephisto
@Mnephisto Жыл бұрын
At 14:00 or so you mention semantic equivalence of different list comprehension forms. Could you explain what semantic equivalence is? Do you mean to say that given the same input it will produce the same output but may differ in memory usage and performance? Or do you mean exact equivalence, where A and B compile to the same bytecode?
@mCoding
@mCoding Жыл бұрын
I mean the first one, they will produce equal lists, throw exceptions in the same situations, create the objects in the same order, etc, as long as you aren't doing crazy introspection stuff. They are not exactly equivalent, and they produce different bytecode.
@mattos000
@mattos000 Жыл бұрын
Video Request: Can you do a video on memoization and caching generators?
@PetrSzturc
@PetrSzturc Жыл бұрын
Donky chonky were great. :D
@bassycounter
@bassycounter Жыл бұрын
Dude I’ve been stuck trying to understand scope for weeks now, I’m working on a project to help me get the hang of python but oh my GOD, scope is kicking my butt because nothing I want to do seems to work. Might have to watch this video 20+ times to finally FULLY understand, even then maybe not cause my brain is mush lmao
@fejfo6559
@fejfo6559 Жыл бұрын
Are there multiple non local scopes for deeply nested functions? like x= "global" def f1(): x = "non local 1" def f2(): x="non local 2" def f3(): x="local" f3() f2() print(x) f1() are non local 1 and 2 assigned to the same cell? If so this should print non local 2. The code prints non local 1 so there are multiple non_local scopes?
@domogdeilig
@domogdeilig Жыл бұрын
You have misunderstood. Since x is defined in function f1 it will look for x there, or if it wasn't defined there it will look for global, it will only peel outwards. This has to be the case or you would be afraid calling a function anywhere because it might rename global or local variables. This would be a nightmare. It can happen that code affects variables in outer functions, but that is either when specifying it or when you have something like an array where it is refrencing a memory location. Here is an example of the latter: import numpy as np y = np.ones(3) def f1(): # y+=1 gives UnboundLocalError: local variable 'y' referenced before assignment data = y # Reference to array from global variable y print(y) [1,1,1] def f2(): data2 = data data2 +=1 # Changes the global variable y and nonlocal variable data data2 = 1 # Doesn't change the global variable y data +=1 print(y) # [2,2,2]! f2() print(y) # [3,3,3]!!! f1() print(y) # [3,3,3] ################ Now in f2 it doesn't matter what you call data2, as long at isn't the same as the variable your referencing (you get an error). So no y=y or data=data, but data=y or y=data will reference global variable or the non local variables.
@hikariatama8543
@hikariatama8543 Жыл бұрын
What if we replace line `z = "donkey z"` with `locals()["z"] = "donkey z"`? Will it still works the same, or will the compiler ignore this definition? 🧐
@alexsischin2107
@alexsischin2107 Жыл бұрын
16:40 What I expect to be printed print(fun()) > 1 next(gen) print(fun()) > 2 next(gen) print(fun()) > 3
@itsmaxim01
@itsmaxim01 4 ай бұрын
in level 2 isn’t the x assignment supposed to be in the if statement’s scope rather than the function scope?
@hassansyed6087
@hassansyed6087 24 күн бұрын
Sorry, I'm moderately new to python so I might be asking a silly question here but at 4:40: - You said that the local x gives a run time error simply because you didn't assign a value My Question --> Isn't the string itself called "local x" the value assigned to that local x? Maybe I missed something. thanks !
@oida10000
@oida10000 Жыл бұрын
Python has the key word global which allows you to look up global variables while reusing the name locally or not? Is there something similar for the outer scope and if not why not?
@TheYxccxy
@TheYxccxy Жыл бұрын
Are you looking for the keyword: nonlocal ?
@matsim0
@matsim0 Жыл бұрын
Did you watch the video? Nonlocal is explained in detail with examples.
@thanhdo1963
@thanhdo1963 Жыл бұрын
when I see the thumbnail , I was instantly having the JS-phobia :)
@TheWyrdSmythe
@TheWyrdSmythe Жыл бұрын
Inner functions are great for currying multi-parameter functions into single-parameter functions.
@ch4sethe5un
@ch4sethe5un Жыл бұрын
got x, y right, but z was a bit confusing why it gets sent with `inner`
@bp56789
@bp56789 Жыл бұрын
How did you learn Python to this level?
@zohnannor
@zohnannor Жыл бұрын
You should've mention this also: >> [(lambda: x)() for x in range(10)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [f() for f in [lambda: x for x in range(10)]] [9, 9, 9, 9, 9, 9, 9, 9, 9, 9] >>>
@mCoding
@mCoding Жыл бұрын
I mean, as with many of the examples in the video, please don't do this 😉 buuuut yes even these examples follow the same rules!
@zohnannor
@zohnannor Жыл бұрын
@@mCoding yes, I love to surprise my friends by this little piece of code and then explain them closures and variable lookup rules :)
@NicolasChanCSY
@NicolasChanCSY Жыл бұрын
@zohnannor Thanks for your example! It may be a short example, but I have stared at it for 5 minutes to understand it 😂 For those who cannot understand the 2nd line in short time, here is the explanation (Thanks James for his video explanation): - [lambda: x for x in range(10)] can be slightly rewritten for clarity by [(lambda: x) for x in range(10)]. - The list comprehension can then be rewritten as: def gen(): for x in range(10): yield lambda: x list(gen()) - The list comprehension can further be rewritten as: def gen(): for x in range(10): def inner(): return x yield inner list(gen()) - x in inner() refers to the x in the enclosing for-loop, as decided in compile time, but the value lookup is at runtime. - x is first 0, and then 1, and then ..., and finally 9, because of range(10). - list(gen()) is a list of inner's, each with a x in their closure, pointing to the same x in the enclosing for-loop, but the value has not been looked up. - When the code runs f() for f in list(gen()), it assigns inner to f, and runs inner(). - The value of x is now looked up, and the latest value of x is 9. And this apply to all 9 inner's. - Thus, [9, 9, 9, 9, 9, 9, 9, 9, 9]. I hope I did not miss anything. Thanks again for your example, zohnannor! And thanks to James for his video!
@zohnannor
@zohnannor Жыл бұрын
​@@NicolasChanCSY yes, pretty much like that! I also show this code after, as an explanation (notice `id(x)`): >>> [(lambda: id(x))() for x in range(10)] [2117705466064, 2117705466096, 2117705466128, 2117705466160, 2117705466192, 2117705466224, 2117705466256, 2117705466288, 2117705466320, 2117705466352] >>> [f() for f in [lambda: id(x) for x in range(10)]] [2117705466352, 2117705466352, 2117705466352, 2117705466352, 2117705466352, 2117705466352, 2117705466352, 2117705466352, 2117705466352, 2117705466352] and then, this: >>> l = [lambda: x for x in range(10)] >>> l[0].__closure__[0] # 0 index >>> l[1].__closure__[0] # 1 index, same address >>> l[0].__closure__[0].cell_contents 9 >>> And also, I think it's a good thing to not only show some confusing, "broken" code as it may seem, but also a way to circumvent the shown behavior, in this case, it's this: >>> [(lambda x=x: x)() for x in range(10)] # notice `x=x`, `x` becomes local variable [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [(lambda x: x)(x) for x in range(10)] # notice `x` being passed as an argument [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> 🙂
@NicolasChanCSY
@NicolasChanCSY Жыл бұрын
@@zohnannor Excellent follow-up code to show the inner working (pun intended)!
@prosodyspeaks4036
@prosodyspeaks4036 8 ай бұрын
thaanks
@JorgeLuis-ts6qp
@JorgeLuis-ts6qp Жыл бұрын
Can you talk about how objects work in Python 3.11? According to Pablo Galindo object instances don't have a dictionary anymore. This leads me to wonder: do slots save memory compared to a regular class?
@PanduPoluan
@PanduPoluan Жыл бұрын
__slots__ does save memory. Not much, like 100-ish bytes per instantiated object. But if you create LOTS of those objects, the differences add up, sometimes significantly. That said, if you handle thousands or millions of objects, those objects likely hold immutable datapoints to be processed. Consider not writing your own custom classes, but use NamedTuple or Dataclasses with slots=True
@petic3198
@petic3198 Жыл бұрын
can someone explain me what does the "return inner " stament do in minute 0:32 ?I do not get it
@mCoding
@mCoding Жыл бұрын
Inner is a function. In python, functions are objects just like ints and strings. Just like I can return my_int_varable I can also return my_function. In this case, the name of my function variable is "inner".
@petic3198
@petic3198 Жыл бұрын
@@mCoding thanks,I got it!
@roeesi-personal
@roeesi-personal Жыл бұрын
It's nice to know what happens behind the scenes, but as an experienced python programmer with some common sense and generalization abilities I could understand what level 6 does even without knowing about __closure__s. The x is taken from the globals and not from chonky because it makes no sense that a variable defined inside one function will affect another funcction it only calls, and the z is from after the inner function definition in exactly the same way that a function can use functions defined after it, just with a variable instead of a function and in a local and not a global scope.
@b4ttlemast0r
@b4ttlemast0r Жыл бұрын
4:06 so if you get the value of a variable, it refers to the global variable, but when you use an assignment, it suddenly refers to a newly created local variable instead of changing the value of the global one? How does that make sense; it seems really unintuitive that the variable name itself refers to completely different things depending on whether you are assigning or just getting a value. I really wish python just used a different syntax for declaring a new variable than for assigning to an existing one. Edit: 14:33 ok so it _is_ actually still possible to assign to a nonlocal/global variable, I was wondering if that's even possible. What I said before still applies.
@protonmaster76
@protonmaster76 Жыл бұрын
What would be a legitimate reason to have nested functions like this?
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
Function factories.
@vladrootgmailcom
@vladrootgmailcom Жыл бұрын
As a developer with 20 years professional experience I can guarantee a 100% getting bitchslapped by some of your colleagues if you ever write code with these crossovers within and everyone around will give him 100% support. You CAN do that in any language, but ONLY for fun and giggles. NEVER as a production code.
@maxaafbackname5562
@maxaafbackname5562 Жыл бұрын
I don't understand why you ever want to use Python for production code whatsoever...
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
Au contraire, lexical binding is one of the fundamental concepts that every programming language should have. Yes, I have taken major advantage of it in production code. Check out
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
DBussy, my pure-Python binding for libdbus.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
It constructs proxy-interface classes for D-Bus RPC interfaces on the fly, making heavy use of lexical binding and closures to define the proxy methods. If you think you can come up with some simpler way of doing it, by all means post your code as well. Then we can compare.
@vladrootgmailcom
@vladrootgmailcom Жыл бұрын
@@lawrencedoliveiro9104 bro, probably no one told you that it is bad to be proud of writing a spaghetti code.
@gicknardner
@gicknardner Жыл бұрын
What about
@gicknardner
@gicknardner Жыл бұрын
Comments within comments?
@bereck7735
@bereck7735 Жыл бұрын
Nice
@vikingthedude
@vikingthedude Жыл бұрын
It’s functions all the way down baby
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
There’s a name for that in mathematics: “λ-calculus”.
@vikingthedude
@vikingthedude Жыл бұрын
So apparently I've seen this video before. And yet I still added it to my watch later playlist and had it there for weeks now
@aouerfelli
@aouerfelli Жыл бұрын
Thank you very much. This is an excellent video! At first, when I saw x = "global x" I thought you will do something like exec(x).
@DuarteMolha
@DuarteMolha Жыл бұрын
In recent videos i have seen a lot of people defending functional languages instead of oo. But the idea of using closures to store values for me is so much more complex. It might have since powerful features but for me it is a complete nonstarter.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
In Python, functions and classes are first-class objects. That means you can operate with them at run-time just like you can with other types like numbers, strings etc. This idea did not originate with Python, but with certain advanced languages from the early days of computing, like LISP. It was recognized to be very useful, which is why Python is designed with it as a core idea.
@DuarteMolha
@DuarteMolha Жыл бұрын
@@lawrencedoliveiro9104 i know. I program in python but never needed to use closures for anything. I am sure it is probably powerfull ... but i cannot get my head around it when there are easier more straighforward ways of doing things... at least for me
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
@@DuarteMolha Function factory for code reuse.
@DuarteMolha
@DuarteMolha Жыл бұрын
@@lawrencedoliveiro9104 factory pattern? Not sure i undertand. The factory parttern is usualy given as an example of oo programing. If you mean more exotic code than maybe. I have never seen a reason to use a closure. But sure ... I am talking about my own experience. I think the examples on this video do show how it can be a complex beast
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
@@DuarteMolha Look at my HarfPy module. It makes heavy use of both function factories and class factories.
@xxlarrytfvwxx9531
@xxlarrytfvwxx9531 Жыл бұрын
Afaik windows 11 actually got rid of the vertical taskbar for the "centered" startbar. The setting to move the taskbar to vertical on the left I think was in the display settings of Win10, if someone still has win10 please reply.
@gloweye
@gloweye Жыл бұрын
If I saw that first example in a code review, I'd reject it merely based on the shadowing or the names. Sure, it's all logical, but readable is important in code. If a refactor with good naming isn't possible, I'd demand at the very least a comment or docstring explaining the developer intent and function. In Python, your tools may be powerful, but [uncle ben quote].
@asddsaasdfg2846
@asddsaasdfg2846 Жыл бұрын
Man python sometimes get wild because of how scopes works
@rb1471
@rb1471 Жыл бұрын
What if I wanted to do something like this to print out non local 1? x= "global" def f1(): x = "non local 1" def f2(): x="non local 2" def f3(): nonlocal nonlocal x #??????????? return x return f3() return f2() print(f1())
@mCoding
@mCoding Жыл бұрын
There is no syntax for this in Python, you would need to pick another variable name besides x 😀
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
8:37 The CS term is “environment”, but perhaps the Python folks wanted to minimize confusion with the POSIX concept of “environment variables”. That is, assuming they had a non-capricious reason for using the nonstandard term ...
@fdc_zotei52
@fdc_zotei52 10 ай бұрын
Plans within the plans...
@bereck7735
@bereck7735 Жыл бұрын
Discord gang.
@anasbadr9372
@anasbadr9372 Жыл бұрын
Yes.
@bereck7735
@bereck7735 Жыл бұрын
Sup anas
@zlorf_youtube
@zlorf_youtube Жыл бұрын
Im a member of the "I just opened KZfaq, accidently" Gang
@foo0815
@foo0815 Жыл бұрын
The problem in Python is that assignment implicitly declares a local variable, so one has to declare (by nonlocal or global) if this is not wanted. Other languages like Scheme or Lua do this better, local Variables have to be declared explicitly.
@PatrickSteil
@PatrickSteil Жыл бұрын
Yes but why would you want to define a function within a function?
@mCoding
@mCoding Жыл бұрын
Great question! The most common reason is to define some kind of higher order function. A common example would be decorators like @dataclasses.dataclass or @functools.wraps
@ygorko3834
@ygorko3834 Жыл бұрын
Am i the only one here more surprised that variables in python apparently have function scope instead of block scope?
@TN29
@TN29 Жыл бұрын
Oh shit divide by zero error.
@hipekhop
@hipekhop 10 ай бұрын
That's why I feel like Python, such great language, is broken. I am dreaming of Python 4 with pointers and fixed scopes. You pass arguments like in C++, without thinking about mutable, scopes etc. If you don't want to work on the copy, then pass it by reference - ALL PROBLEMS SOLVED in the most elegant, readable way. That's the point of python, isn't it?
@floydpowell486
@floydpowell486 2 ай бұрын
this made me more confused lol
@TheKimpula
@TheKimpula Жыл бұрын
Yeah well, even tough you put the two rules next to each other there are still two of them :D
@stijndeboer3721
@stijndeboer3721 Жыл бұрын
Thirst
@markcuello5
@markcuello5 Жыл бұрын
SECURITY
@jenaf4208
@jenaf4208 Жыл бұрын
Yeah pythons way of doing closures is counter-intuetive to me. I would lock in any variables to their values at runtime when the it is defined. Changing variables later would not change the defined function.
@qqleq
@qqleq Жыл бұрын
I was going to learn Python but now I am not so sure. Scopes should be clear, simple, and clearly defined by the programmer. The fact that you need this video telling us it is very confusing and you need to learn a lot of rules, tells me something is structurally wrong with scopes in Python.
@RuslanKovtun
@RuslanKovtun Жыл бұрын
Ok, I will NEVER do this.
@jyvben1520
@jyvben1520 11 ай бұрын
luckily my brain does not function like that
@69k_gold
@69k_gold Жыл бұрын
It's ethically illegal to use global variables inside a nested function.
@mCoding
@mCoding Жыл бұрын
This should be a PEP 😅
@andrewburtnett1445
@andrewburtnett1445 Жыл бұрын
Sounds cool, but your pull request is not approved 👎
@realedna
@realedna Жыл бұрын
Lvl 7 has not really a point to it: 1. You use "if False:" to explicitly create an "empty" state of that variable, but then you later might complain about it or get bitten by it!? UNLIKELY 2. You would usually go with a default value of "None" with no need of "If False" and then be pretty safe. So, I don't understand why you would fear someone could unintentionally create a local variable as an empty cell, instead of a simple local variable initialized to "None".
@realedna
@realedna Жыл бұрын
It's like shooting yourself in the foot by knowing the internals of a language!?
@mCoding
@mCoding Жыл бұрын
The cell would still be emtpy if False was replaced by any condition that happened to be falsey at runtime. This is a very common bug in practice, though the rest of example 7 is of course just an exercise to test your understanding of the rules.
@devin3944
@devin3944 10 ай бұрын
So basically, dont make your code as unnecessarily complicated and contradictory like this
@tommclean9208
@tommclean9208 Жыл бұрын
if you ever think have you do this: grow up
@mrob456
@mrob456 Жыл бұрын
at 4: 24 you are passing something false but i see no change in code so what are you passing that is false?
@mCoding
@mCoding Жыл бұрын
Off screen I have a main function calling the code with True then with False.
@qexat
@qexat Жыл бұрын
LOAD_GLOBAL 0 (x)
@nathanwalker4969
@nathanwalker4969 Жыл бұрын
cc @corridorcrew
Python Generators
15:32
mCoding
Рет қаралды 128 М.
8 things in Python you didn't realize are descriptors
14:21
Eccentric clown jack #short #angel #clown
00:33
Super Beauty team
Рет қаралды 25 МЛН
Dynamic #gadgets for math genius! #maths
00:29
FLIP FLOP Hacks
Рет қаралды 18 МЛН
ONE MORE SUBSCRIBER FOR 6 MILLION!
00:38
Horror Skunx
Рет қаралды 14 МЛН
Would you like a delicious big mooncake? #shorts#Mooncake #China #Chinesefood
00:30
Uniscale: Intro
5:32
Uniscale
Рет қаралды 48
What is Scope in Python??
10:25
b001
Рет қаралды 43 М.
Every Python dev falls for this (name mangling)
14:11
mCoding
Рет қаралды 135 М.
Python's collections.abc | InvertibleDict
14:00
mCoding
Рет қаралды 44 М.
WHY IS THE STACK SO FAST?
13:46
Core Dumped
Рет қаралды 132 М.
Python __slots__ and object layout explained
10:16
mCoding
Рет қаралды 90 М.
do you know how "return" works under the hood? (are you SURE?)
5:08
Low Level Learning
Рет қаралды 345 М.
8 Design Patterns | Prime Reacts
22:10
ThePrimeTime
Рет қаралды 380 М.
Will the battery emit smoke if it rotates rapidly?
0:11
Meaningful Cartoons 183
Рет қаралды 1,1 МЛН
Which Phone Unlock Code Will You Choose? 🤔️
0:14
Game9bit
Рет қаралды 12 МЛН
cool watercooled mobile phone radiator #tech #cooler #ytfeed
0:14
Stark Edition
Рет қаралды 7 МЛН
Nokia 3310 versus Red Hot Ball
0:37
PressTube
Рет қаралды 3,8 МЛН
Топ-3 суперкрутых ПК из CompShop
1:00
CompShop Shorts
Рет қаралды 333 М.
The power button can never be pressed!!
0:57
Maker Y
Рет қаралды 55 МЛН