Python tutorial: Variable Scopes & Namespaces - global/local/nonlocal | Explained with animations

  Рет қаралды 21,468

Sreekanth

Sreekanth

3 жыл бұрын

The variables you define in your Python code has a lexical scope where they are valid. These scopes are categorized as local, enclosing, global and built-in scopes.
Python language also provides a few keywords like global and nonlocal to modify the behaviour of its variable name resolution.
This video tries to explain how scopes and namespaces work in different scenarios.
How variables work in Python: • How variables work in ...
Credits: The contents of this video are mostly from my notes on Dr. Fred Baptiste's course www.udemy.com/course/python-3...

Пікірлер: 32
@grifo_-cyan7123
@grifo_-cyan7123 Жыл бұрын
I was having trouble understanding this concept before, but the way you explained it visually really helped me! Thank you for this video.
@sidlas420
@sidlas420 3 жыл бұрын
Simply brilliant. First time learning about built-in scope and nonlocal keyword
@akmzahidulislam2764
@akmzahidulislam2764 2 жыл бұрын
These videos are for serious learners, who wants to become professional programmers. Cannot think of anything better. Thanks and sincere gratitude for your rigorous scholarship that you have shared with us.
@rainforest3233
@rainforest3233 Жыл бұрын
Thank you so much! I hope KZbin recommends your channel more to more people!
@apamwamba
@apamwamba Жыл бұрын
You are a genius. I at last understand global , local , nonlocal .. being a visual person. Your method of VISUAL teaching is UNIQUE. Do you have plans to produce full tutorials even on a paid plan. I would definately subscribe. Your work is simply EXELLENT. The best explanations i have come across so far. Thanks
@miloudiriyad3671
@miloudiriyad3671 6 ай бұрын
You are the best, i will be more than happy to purchase your training if you make one.
@gouthamkaranam3825
@gouthamkaranam3825 Жыл бұрын
thanks a lot sreekanth.. i referred other materials as well. but your video explanations made the pint clear in one go.
@xiaoweilu5349
@xiaoweilu5349 7 ай бұрын
This is so clear explaining that it's hard to not understand!
@expansivegymnast1020
@expansivegymnast1020 Жыл бұрын
Everyone should watch this. This is the Python that's just thrown at you without an explanation of how it works.
@cb9493
@cb9493 2 ай бұрын
finally, I understood the concept. thank you so much for explaining the 'local assignment entry masks the global namespace'. it makes a lot sense. Ta~~'
@southof40
@southof40 2 жыл бұрын
Very good, thanks for your work producing this video. Taught me some new things and reminded me of some old things. I found the voice-over a little fast for easy comprehension so I switched the playback to 0.75 speed which, for me, worked better. Thanks again.
@user-oh4wd9vk4s
@user-oh4wd9vk4s 11 ай бұрын
brilliant, love the way you tell things.
@sajjadhossan7972
@sajjadhossan7972 2 жыл бұрын
Brilliant animation. Thanks for this video and your efforts. I wish One day you'll obviously success for video
@debgandharghosh3981
@debgandharghosh3981 3 ай бұрын
The concepts are explained in a beutiful manner ... I was just wondering what the rush was all about ?
@itzcallmepro4963
@itzcallmepro4963 Жыл бұрын
Perfect , i Couldn'tt understand it before thx
@venentium
@venentium 2 жыл бұрын
love you channel, please not leave us, we want more videos
@pretamane9646
@pretamane9646 2 жыл бұрын
It helps alot, can't afford patreon or donation, put some ads in, we are okay with that
@django3861
@django3861 Жыл бұрын
5:46 good lecture thx. I have a question, at this point I think that outet function's namespace cannot have a value for "x" because outer function is not called(executed)
@Han-ve8uh
@Han-ve8uh 2 жыл бұрын
You mention at the end that nonlocal cannot be chained to global. I wish to ask about how nonlocal chain to each other. In code below, why is outer's x finally printed to be 2 and not 1? outer's x seems to know inner2 (2 levels inside) has updated x too. Does this mean inner1's nonlocal points to x in outer, and inner2's nonlocal points to inner1, therefore causing inner2's nonlocal to also point to outer, so all 3 scopes see the same x value that the address of x resolves to? (I can see they are pointing to same id() if using mutables like list for x) def outer(): x=0 def inner1(): nonlocal x x+=1 def inner2(): nonlocal x x+=1 inner2() inner1() print('x after:',x) # why is this not 1 outer()
@akmzahidulislam2764
@akmzahidulislam2764 2 жыл бұрын
You already got it. The same id confirms a single object has been created, and all non-local x points to the same object. With each level's execution, the value of the object is updated; Sequence is 0, 1, 2.
@johnaweiss
@johnaweiss 5 ай бұрын
2:15 Do you mean you code will use the "masking" function (your custom `print` function)? The native `print` is masked by your custom `print`. So your custom `print` is "masking the native `print`. The custom `print` is a "masking function". The native `print` is a "masked function" .
@Woeden
@Woeden 2 жыл бұрын
ty!
@wakuwaku4853
@wakuwaku4853 Жыл бұрын
hey! what does print is masked by print? at 2:22
@NadeemRasoolpro
@NadeemRasoolpro Жыл бұрын
Hi Sreekanth your Python tutorials are indepth and awesome, keep it going. Please can you recommend any python book which teachs indepth python concepts.
@sreekanthpr
@sreekanthpr Жыл бұрын
"Fluent Python" is the only book I've read. Mostly, I just read blogs or watch talks by James Powell and Raymond Hettinger. I'm not a Python programmer, at work I use Go. I was re-learning Python with my knowledge in other programming languages.
@NadeemRasoolpro
@NadeemRasoolpro Жыл бұрын
@@sreekanthpr I appreciate your quick and helpful response. It is strange that you are not python programmer but still you have got indepth knowledge of Python. Keep up the good work!!!
@KoenCuijp
@KoenCuijp Жыл бұрын
Great videos, thanks a lot! Great material to improve our "under the hood" understanding. One part that surprised me was that with using the global keyword in a local scope the variable gets created in the global scope if it doesn't exist there yet (kzbin.info/www/bejne/jYq9o3-qe7N3jKs), while with the nonlocal keyword the variable doesn't get created in an outer non-global environment if it doesn't exist there yet (kzbin.info/www/bejne/jYq9o3-qe7N3jKs). Do you know if this was a conscious implementation decision by the Cpython community? I can imagine it being decided like this because it would be a bit ambiguous in which outer scope the variable should be created (while with the global keyword there's explicitly 1 scope we're targeting).
@dangerousideas
@dangerousideas Жыл бұрын
I love the illustrations and information. Although I'd really prefer this be explained by a human tho...
@leha9538
@leha9538 Жыл бұрын
I think you just rephrased and summarized what are taught from Fred Baptiste's courses on Python Deep Dive series in Udemy. Not only this video but many others from you. You should credit him, refer to his courses and say what you did to create these videos. For audiences, as this channel gets the most materials from paid courses (they are very cheap, though) and summarizes them (sometimes not adequately), I suggest you should go to the original course to understand even more about Python which is excellently taught by Dr. Fred Baptiste.
@sreekanthpr
@sreekanthpr Жыл бұрын
Updated description of all Python videos. Wasn't sure since its a paid course.
@mukundachlerkar8021
@mukundachlerkar8021 2 жыл бұрын
U talk so fast. This is not done.
@akmzahidulislam2764
@akmzahidulislam2764 2 жыл бұрын
Go to the right-hand upper corner, click at..., find play speed. Set to 0.75. That should be. If you still cannot follow, this class level is not suitable for you. Sorry
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 4,8 МЛН
UFC Vegas 93 : Алмабаев VS Джонсон
02:01
Setanta Sports UFC
Рет қаралды 224 М.
Increíble final 😱
00:37
Juan De Dios Pantoja 2
Рет қаралды 107 МЛН
What is Scope in Python??
10:25
b001
Рет қаралды 45 М.
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 427 М.
Closures in Python | Explained with animations
8:08
Sreekanth
Рет қаралды 23 М.
Python Scope tutorial for Beginners
19:50
Dave Gray
Рет қаралды 6 М.
How variables works in Python | Explained with Animations
18:39
Python Namespaces
15:13
John Philip Jones
Рет қаралды 9 М.
What is a CLOSURE and how to use it in Python?
4:04
Python for Everyone
Рет қаралды 11 М.
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,1 МЛН
This INCREDIBLE trick will speed up your data processes.
12:54
Rob Mulla
Рет қаралды 258 М.
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 4,8 МЛН