What is Scope in Python??

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • ⭐ Join my Patreon: / b001io
    💬 Discord: / discord
    🐦 Follow me on Twitter: / b001io
    🔗 More links: linktr.ee/b001io
    Background Music:
    Gentle Lo-Fi Vlog Background Music | Cookies by Alex-Productions | onsound.eu/
    Music promoted by www.free-stock...
    Creative Commons / Attribution 3.0 Unported License (CC BY 3.0)
    creativecommon...

ความคิดเห็น • 60

  • @armpitpuncher
    @armpitpuncher ปีที่แล้ว +28

    Would have been good if you covered how scopes relate to loop bodies and if/else blocks, because it's not intuitive in python. For example, in every language which I'm familiar with besides python, a variable declared inside an if block is scoped to that block, and not accessible outside it. This is not the case in python.

  • @AntonioZL
    @AntonioZL ปีที่แล้ว +10

    2:34 while this is indeed obvious for anyone experienced with interpreted languages, it could be confusing for begginers coming from compiled languages, like C, which is quite commonly used in intro to programming classes, so it's worth to point it out.

    • @epsi
      @epsi ปีที่แล้ว +1

      I think you meant it's obvious for anyone experienced with compiled languages.
      Some interpreted languages like JavaScript, awk, various BASIC dialects, and some flavors of shell script do not require variables to be defined before they are used.
      I've never encountered a compiled language that allows you to use a variable without first defining it.

  • @MoonFrogg
    @MoonFrogg ปีที่แล้ว +10

    these are VERY important things to learn, love the video and thanks for the clear + concise explanation!!

  • @NickWrightDataYT
    @NickWrightDataYT ปีที่แล้ว +8

    Great stuff! Good to learn more about how Python "thinks" lol

  • @aliplayer1
    @aliplayer1 ปีที่แล้ว +5

    Love the videos. Concise yet full of information for new learners. Thanks.

  • @chsh9993
    @chsh9993 8 หลายเดือนก่อน

    Your explanation of coding is truly ingenious. Suddenly, all those encrypted coding lines aren't that confusing anymore.

  • @SHAYANGUHA
    @SHAYANGUHA 2 หลายเดือนก่อน +1

    really informative
    😍😍😍😍😍

  • @nadiaezzarhouni300
    @nadiaezzarhouni300 ปีที่แล้ว +2

    Bro you are insane, can you do a python course , by the way where do you get all this knowledge

  • @LilJollyJoker
    @LilJollyJoker 8 หลายเดือนก่อน +1

    Thank you for this video!

  • @porlando12
    @porlando12 ปีที่แล้ว

    Wow this was awesome. Your channel is criminally underrated!

  • @user-pf8ww2im6f
    @user-pf8ww2im6f ปีที่แล้ว

    Man your video really helps. I couldn't understand the explanation on the official doc. Tyvm!

  • @bab4953
    @bab4953 8 หลายเดือนก่อน

    ur channel deserves more subs. Your content is amazing👍

  • @montagetm
    @montagetm ปีที่แล้ว +2

    What if you want to reference a local variable two scopes out, how would you do that? Nonlocal only references the previous scope and global only references the global scope.

    • @epsi
      @epsi ปีที่แล้ว +1

      def outer():
      x = 1
      def inner():
      # x = 2
      def func():
      nonlocal x # refers to x=1
      If you uncomment the x=2 line, you'd be referencing that x variable instead, and you'd be forced to rename one of those two x variables to be able to reference both of them.
      The nonlocal keyword is kind of a hack because Python is designed in a way that it prefers creating shadowed variables instead of unexpectedly modifying variables from outside scopes.
      Unfortunately, that means you encounter variable shadowing issues more frequently if you don't name things uniquely enough, along with the surprising error that is the answer to the poll question.

    • @montagetm
      @montagetm ปีที่แล้ว

      @@epsi so the only way is basically to rename them or remove them

    • @epsi
      @epsi ปีที่แล้ว +1

      @@montagetm
      Yes, but that's true for any other language as well.
      Variable shadowing: just don't do it.

  • @SusanAmberBruce
    @SusanAmberBruce ปีที่แล้ว

    Probably one of the most important videos to watch if someone is new to python

  • @mind_of_a_darkhorse
    @mind_of_a_darkhorse ปีที่แล้ว +1

    Great explanation of scope! You made it very easy to understand!

  • @sayanta01
    @sayanta01 3 หลายเดือนก่อน

    thanks man

  • @BiyHappy
    @BiyHappy ปีที่แล้ว

    Thanks man what a smooth explanation

  • @naifal-ghamdi483
    @naifal-ghamdi483 ปีที่แล้ว

    Best scope explanation ever, many thanks to you

  • @harshaljani2266
    @harshaljani2266 22 วันที่ผ่านมา

    If there were 4 functions nested then would there be a way to access the 2nd function’s variable in the innermost 4th function?

  • @killerboyak
    @killerboyak 8 หลายเดือนก่อน

    Amazing Explanation , Thank you.

  • @tobiasm161
    @tobiasm161 ปีที่แล้ว +1

    Great explanation with examples ! Bravo
    I would love to see kind a approach about OOP. Because sometimes there is an overkill to use it. But otherwise its good practice i think. What do you think or whats your approach. I tend to do less OOP cause often "one time" use of my scripts

  • @adammarley5616
    @adammarley5616 8 หลายเดือนก่อน

    Beautifully taught!

  • @elancedin7204
    @elancedin7204 ปีที่แล้ว

    bro literally did a few vids and already 82k subs, gut 4 u

  • @arunalakmal9031
    @arunalakmal9031 6 หลายเดือนก่อน

    Thnak you broo!!!

  • @yazanabdoush7030
    @yazanabdoush7030 5 หลายเดือนก่อน

    Can you share what color theme you are using?

  • @vuIent
    @vuIent ปีที่แล้ว +1

    Good video

  • @alanlado1602
    @alanlado1602 ปีที่แล้ว

    I like the way you explain things. Subscribe button smashed.

  • @birzhanabdikhan8175
    @birzhanabdikhan8175 10 หลายเดือนก่อน

    1:37 by first-indentation he means that variable was defined outside of classes and functions. This part confused me too because I though that he meant 1st line.

  • @soniablanche5672
    @soniablanche5672 9 หลายเดือนก่อน

    I knew of "global", but I didn't know about "nonlocal"

  • @KrAsHeDD
    @KrAsHeDD ปีที่แล้ว

    Thanks, you answered my awaited long question.

  • @faisal99129
    @faisal99129 11 หลายเดือนก่อน

    very good explanation... very helpful

  • @udishe
    @udishe ปีที่แล้ว

    Ty

  • @yaraa.a5237
    @yaraa.a5237 8 หลายเดือนก่อน

    the music is fire it got me dancing. and it remind me of minecraft

  • @fatiheee1996
    @fatiheee1996 ปีที่แล้ว

    Thank you for this great explaination!

  • @samsungcurved
    @samsungcurved 20 วันที่ผ่านมา

    Which theme you are using?

  • @Nicky411
    @Nicky411 11 หลายเดือนก่อน

    Lovely video!

  • @charlesrussell6183
    @charlesrussell6183 ปีที่แล้ว

    great explanation

  • @karthiksundaram544
    @karthiksundaram544 ปีที่แล้ว

    Great stuff

  • @mossdem
    @mossdem ปีที่แล้ว

    Could you do a video which does a clear breakdown of classes?

  • @eric_d
    @eric_d ปีที่แล้ว

    I didn't participate in the poll, but from very recent experience I knew it would be an error as soon as I looked at it. A couple weeks ago I was working on a script where I wanted to redefine a global variable, but I didn't use global inside the function. What's even weirder is I tried doing it with 2 different variables, and only the second one gave an error. The first variable was redefined, and printed what I was expecting, but then I got the error when trying to print the second variable. This may be a bug in Python 3.10 that I need to further explore.

  • @TMinusRecords
    @TMinusRecords ปีที่แล้ว +2

    9:17 This whole thing is a disgusting mess

  • @swaggitypigfig8413
    @swaggitypigfig8413 ปีที่แล้ว +2

    Can you do a video on 360 no scope in Python next?

  • @Joe_Brig
    @Joe_Brig ปีที่แล้ว

    2:30 not obvious if you coming from a compiled language.

  • @vvv3876
    @vvv3876 ปีที่แล้ว

    I learned this on practice, i wish i viewed this video before

  • @saadqayyum4739
    @saadqayyum4739 ปีที่แล้ว

    i though python was interpreded so it reads code line by line, shouldn't it print x and then make a local variable x instead of giving error

  • @OsirisRD
    @OsirisRD ปีที่แล้ว

    what is your theme vscode ?

  • @xcc4010
    @xcc4010 ปีที่แล้ว

    What is the name of the theme?

  • @dongchen7940
    @dongchen7940 11 หลายเดือนก่อน

    There is no compile time in python, it is called interpret.

  • @ismahelo
    @ismahelo ปีที่แล้ว

    What font is that? And what theme?

    • @amoghgc8712
      @amoghgc8712 6 หลายเดือนก่อน

      SynthWave '84

  • @reynaparker2961
    @reynaparker2961 2 หลายเดือนก่อน

    5:26

  • @magdalenacoman9120
    @magdalenacoman9120 5 หลายเดือนก่อน

    bru put the music louder pls

  • @KeithKazamaFlick
    @KeithKazamaFlick ปีที่แล้ว

    dont use global guys.

  • @Hotwire_RCTrix
    @Hotwire_RCTrix 5 หลายเดือนก่อน

    This language is conflicting coming from C++. We were taught that this kind of programming was for those ignorant BASIC programmers of the 70s. 😳

  • @maker0824
    @maker0824 ปีที่แล้ว

    Too basic for me

  • @pppluronwrj
    @pppluronwrj ปีที่แล้ว

    Js scope is better