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

Sdílet
Vložit
  • čas přidán 13. 09. 2024
  • 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/...

Komentáře • 33

  • @grifo_-cyan7123
    @grifo_-cyan7123 Před 2 lety +7

    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 Před 3 lety +3

    Simply brilliant. First time learning about built-in scope and nonlocal keyword

  • @rainforest3233
    @rainforest3233 Před 2 lety

    Thank you so much! I hope CZcams recommends your channel more to more people!

  • @akmzahidulislam2764
    @akmzahidulislam2764 Před 2 lety +1

    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.

  • @xiaoweilu5349
    @xiaoweilu5349 Před 10 měsíci

    This is so clear explaining that it's hard to not understand!

  • @apamwamba
    @apamwamba Před 2 lety +2

    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

  • @southof40
    @southof40 Před 3 lety +3

    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.

  • @expansivegymnast1020
    @expansivegymnast1020 Před rokem

    Everyone should watch this. This is the Python that's just thrown at you without an explanation of how it works.

  • @cb9493
    @cb9493 Před 5 měsíci

    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~~'

  • @DrIT-qv1ek
    @DrIT-qv1ek Před 2 měsíci

    Simply Great!

  • @sajjadhossan7972
    @sajjadhossan7972 Před 2 lety +1

    Brilliant animation.
    Thanks for this video and your efforts. I wish One day you'll obviously success for video

  • @miloudiriyad3671
    @miloudiriyad3671 Před 8 měsíci +1

    You are the best, i will be more than happy to purchase your training if you make one.

  • @user-oh4wd9vk4s
    @user-oh4wd9vk4s Před rokem

    brilliant, love the way you tell things.

  • @gouthamkaranam3825
    @gouthamkaranam3825 Před rokem

    thanks a lot sreekanth.. i referred other materials as well. but your video explanations made the pint clear in one go.

  • @debgandharghosh3981
    @debgandharghosh3981 Před 6 měsíci

    The concepts are explained in a beutiful manner ... I was just wondering what the rush was all about ?

  • @itzcallmepro4963
    @itzcallmepro4963 Před 2 lety

    Perfect , i Couldn'tt understand it before thx

  • @Han-ve8uh
    @Han-ve8uh Před 2 lety +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 Před 2 lety

      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 Před 8 měsíci

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

  • @django3861
    @django3861 Před rokem

    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)

  • @venentium
    @venentium Před 2 lety

    love you channel, please not leave us, we want more videos

  • @pretamane9646
    @pretamane9646 Před 2 lety

    It helps alot, can't afford patreon or donation, put some ads in, we are okay with that

  • @NadeemRasoolpro
    @NadeemRasoolpro Před rokem

    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  Před rokem

      "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 Před rokem

      @@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!!!

  • @wakuwaku4853
    @wakuwaku4853 Před rokem

    hey! what does print is masked by print? at 2:22

  • @Noritoshi-r8m
    @Noritoshi-r8m Před 2 lety

    ty!

  • @KoenCuijp
    @KoenCuijp Před 2 lety +1

    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 (czcams.com/video/WYZrLtFNDVI/video.html), while with the nonlocal keyword the variable doesn't get created in an outer non-global environment if it doesn't exist there yet (czcams.com/video/WYZrLtFNDVI/video.html).
    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 Před rokem

    I love the illustrations and information. Although I'd really prefer this be explained by a human tho...

  • @leha9538
    @leha9538 Před rokem

    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  Před rokem

      Updated description of all Python videos. Wasn't sure since its a paid course.

  • @mukundachlerkar8021
    @mukundachlerkar8021 Před 2 lety +2

    U talk so fast. This is not done.

    • @akmzahidulislam2764
      @akmzahidulislam2764 Před 2 lety +3

      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