Python Tutorial: Variable Scope - Understanding the LEGB rule and global/nonlocal statements

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ธ.ค. 2016
  • In this Python Tutorial, we will be going over variable scope in Python. Scope is important because we need to understand it in just about every program we write. It allows us to understand where our variables can be seen from within our program and also what values these variables hold. It also helps with debugging, because scope is a common problem when errors are thrown. Let's get started.
    The code from this video can be found at:
    github.com/CoreyMSchafer/code...
    ✅ Support My Channel Through Patreon:
    / coreyms
    ✅ Become a Channel Member:
    / @coreyms
    ✅ One-Time Contribution Through PayPal:
    goo.gl/649HFY
    ✅ Cryptocurrency Donations:
    Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
    Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
    Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
    ✅ Corey's Public Amazon Wishlist
    a.co/inIyro1
    ✅ Equipment I Use and Books I Recommend:
    www.amazon.com/shop/coreyschafer
    ▶️ You Can Find Me On:
    My Website - coreyms.com/
    My Second Channel - / coreymschafer
    Facebook - / coreymschafer
    Twitter - / coreymschafer
    Instagram - / coreymschafer
    #Python

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

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

    I think many people don't even realize how well-thought-out and well-prepared your videos are. Your step by step explanation skills are unsurpassable

  • @saitaro
    @saitaro 6 ปีที่แล้ว +297

    Corey. Man. It's so great that you exist. Thank you.

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

    Mate, I'm being forced to watch some random Python tutorial at my Python Internship and it's a real pain in the ass to do so once one knows your channel. I just realized that theese might honestly be the best tutorials in the web...

  • @ousmanetall1286
    @ousmanetall1286 2 หลายเดือนก่อน +4

    I have never seen a better explain tutorial of LEGB, thank corey, wish you all the best!

  • @PedroHenrique-ps8kj
    @PedroHenrique-ps8kj ปีที่แล้ว +14

    Amazing work, we are really living a great era of transformation where such high level of information is widely available and accessible , what a blessing !!

  • @gideonfelt2819
    @gideonfelt2819 7 ปีที่แล้ว +168

    Man you are a heaven send! I literally was thinking I need to study scope more since being new to python and programming in general I was having lots of problems, and then Google notifies me about this video being released 10 mins latter, wow its just perfect, absolutely perfect, it even covered everything I was thinking about. Thank You so much.

    • @coreyms
      @coreyms  7 ปีที่แล้ว +16

      Awesome. Glad to hear that!

    • @coreyms
      @coreyms  7 ปีที่แล้ว +12

      Oh wow. Thanks a lot. I really appreciate that.

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

      Agree, he is great teacher..Best in world

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

      Plot twist.. It was Google along who knew both what you thought and @Corey thought.

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

      😂

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

    this video has been an awakening. I don't understand why anyone has come up with such an eloquent explanation as yours before.

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

    1st year compSci student here and this helped in more ways than you can imagine. started coding in delphi so there was no need for Enclosing or Builtins and didnt know why my global values were not changing but thank God for you. Much appreciated 🙏

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

    Just wanted to write this after what you said about global variables (not to overuse them). You are not just teaching stuff, also giving great informations and insights about programming. That's a gem!

  • @thevgancheetah
    @thevgancheetah 4 ปีที่แล้ว +7

    Your teaching skills are truly amazing! I understood everything crystal clear! Thank you so much!

  • @davorm4634
    @davorm4634 7 ปีที่แล้ว +3

    I`ve just start to learn Python and find yours tutorials best here, seriously. ( I look many of them ). Thanks a lot man !

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

    Masterful clarity in simple conversational language. What a relief

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

    corey your videos are so compelling ....before going to interview i refer them ..great work,keep going .you are helping a lots of tech lovers to learn.

  • @Ninja-iq2xt
    @Ninja-iq2xt 6 ปีที่แล้ว +1

    Hey Corey, you cant imagine how helpful this video turned out to me, i was stuck in one project for a long time, but you just made my day, my solution was right into the variable scope, Thanks a lot!!!

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

    Every second of your videos is highly worthwhile Mr.Schafer...Thank you so much and God bless you!

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

    great tutorial. answered so many of my questions. I've been teaching myself python by reading head first python but the author didn't explain the variable scopes, which caused me a lot of confusions when I was writing custom functions.

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

    the only professional content creator of python in youtube.. you rock corey !!

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

    My MAN!!! Thank you so much! First video that explained it where I could understand it. I tried a couple other channels and couldn't get it. I was so confused until I watched this video. I'm new to Python and this helped out a lot. Thank you so much!

  • @soumyazyx
    @soumyazyx 4 ปีที่แล้ว +3

    Thank you Corey for your time and effort. Keep spreading the knowledge.

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

    Clarity in the way you speak is amazing!!

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

    So clearly explained. Thank you for an excellent tutorial!

  • @wizard-28
    @wizard-28 4 ปีที่แล้ว +6

    Awesome video, It cleared out my many doubts. Thumbs up to Mr. Schafer. Keep up the good work. I wish I found you earlier when I was struggling to learn java but I hated Python because I couldn't find a good instructor like you, the instructors I found made this language look very hard but now I have found you, I am changed my ambition from becoming a java developer to a python developer. Love you, Corey.

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

    Corey I have never seen anyone to explain this basic and very important information. Thanks.

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

    This was an excellent way to present this confusing topic - thanks!!

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

    Thank You so much, for all the time you put in to your videos! If ever you do a weeklong coding-bootcamp class I'm there!

  • @Airless_Grind
    @Airless_Grind 3 ปีที่แล้ว

    Every single thing i look up during my python course always leads me to your channel, you're an absolute blessing.

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

    Your tutorials are great, I really appreciate your effort.

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

    Again a very crisp video. I learnt programming in 1994 on IBM mainframe and did work on Cobol/DB2/CICS etc. but soon moved into managerial role. One of my biggest problems with lot of videos is that they all spend lot of time on useless explanations... I need someone like you who is short and crip and to the point(because I know programming as such..but need specifics)... I get every minute worth in your videos with no waste of time.

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

    You are the best teacher to explain the core concepts so neatly. I always check your channel for any content that I need help with. Thanks. God Bless you.

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

    i was experimenting with functions, and this is exactly what i was looking for, thanks a lot man

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

    Always a good day when I see you've released a new video! I'm looking forward to seeing a tutorial on Pandas and Sublime Text Setup for Python Development. All the best, Jacob.

  • @yingbowang3068
    @yingbowang3068 2 ปีที่แล้ว

    that nonlocal is amazing..Thank you Mr Schafer for those excellent tutorials

  • @hussainmadar4
    @hussainmadar4 4 ปีที่แล้ว +4

    Wonderful work @Corey Schafer. I have seen so many tutorials, but your tutorials are the best. Many thanks to @Krish naik for suggesting me your tutorials.

  • @sak8485
    @sak8485 6 ปีที่แล้ว +4

    this tutorial series is awesome, Thank you so much man :)

  • @jazmonark3309
    @jazmonark3309 4 ปีที่แล้ว +3

    People has tutorials..... But YOU are a real teacher who says the truth ... The "shape of my heart" song is meant for you❤

  • @ricardo.fontanelli
    @ricardo.fontanelli 4 ปีที่แล้ว

    Your videos are great for programmers moving from other languages to python, thank you

  • @ManuelShelton
    @ManuelShelton 6 ปีที่แล้ว

    Very clear structured lession about LEGB.

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

    Thank you for such crystal clear explanation.

  • @liesdamnlies3372
    @liesdamnlies3372 7 ปีที่แล้ว +25

    I'm glad you added the part about "Look, that's how it works, but you shouldn't overuse it." Globals are dangerous beasts. Certain (now deceased) Toyota owners learned that the hard way.
    It's an easy thing to have overlooked, but very important.

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

    Just an outstanding explanation. Cheers for Corey. Really grateful for your content.

  • @sharonsnail2954
    @sharonsnail2954 2 ปีที่แล้ว

    Excellent!! Best tutorial I've ever seen on this topic 👍👍👍👍👍

  • @pranavpawar1894
    @pranavpawar1894 4 ปีที่แล้ว

    Thanks Corey for uploading such a wonderful playlist

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

    I totally agree on Julian M's comments. This lecture is very well-planned and highly organized. I came to Corey's video after several stops that I wasn't satisfied much. Happy to find Corey S videos.

  • @the_xibalba
    @the_xibalba 2 ปีที่แล้ว

    I has been frantically searching for a proper tutorial on sending emails using Python. I was literally pulling my hair off by reading and implementing solutions from Stack Overflow which introduced some other bus in my project. I accidentally stumbled upon your channel and boy am I glad that I did. Thank you so much for doing this mate, you are making a difference in the lives of other people, I hope you are doing well in these times. Thank you once again!

  • @supersaiyan0x016
    @supersaiyan0x016 4 ปีที่แล้ว

    Definitely worth it.. You explain far better than my college teacher ...

  • @muhammadkarim4231
    @muhammadkarim4231 3 ปีที่แล้ว

    Loved the way you explained the scope so well

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

    Very clear and easy understanding, thanks !

  • @caydanphoenix9067
    @caydanphoenix9067 3 ปีที่แล้ว

    if i had access to a million mail id's, i would have subscribed this channel with each of them...this is in my opinion #1 channel for python..this video cleared every doubt i had in this topic..tysm dude!!

  • @RameezRaja-qc9fi
    @RameezRaja-qc9fi ปีที่แล้ว

    Thank you so much Corey! This video really helped me to understand this concept completely now!

  • @vinces.9990
    @vinces.9990 4 ปีที่แล้ว

    I go ahead and like your videos before I even finish watching. I already know from previous videos that it will be what i'm looking for.

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

    Great content Corey, Keep up the good work :)

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

    Thank you for this great explanation, I never heard of LEGB and I never noticed or had to use global/nonlocal keywords before, probably because I code in C++ style where global variables are, kind of, forbidden. Thank you again for the good explanation. Now I know.

  • @saidhussainmusawi
    @saidhussainmusawi 2 ปีที่แล้ว

    Thanks a lot Corey, you explained it in a best possible way

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

    Great Video and very informative. Thank you so very much for posting.

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

    It's awesome that your alive
    thanks for this

  • @chenxingliu2675
    @chenxingliu2675 7 ปีที่แล้ว +3

    You tutorials are the best!!!

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

    awesome :) looking forward more videos on Python . thank you

  • @peek-a-boosurprisetoysandf5794
    @peek-a-boosurprisetoysandf5794 6 ปีที่แล้ว +1

    You are one of the best Python instructors on TH-cam! Your tutorials explain complicated concepts in a beautifully succinct and clear manner. Just became a patron! 👍👍👍Thanks for sharing your knowledge with us!

    • @coreyms
      @coreyms  6 ปีที่แล้ว

      Thank you! That really helps a lot!

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

      5 years later, you're still one of the best!

  • @MohSo14
    @MohSo14 7 หลายเดือนก่อน

    How great explanation on this topic. Great job!

  • @ramveerla
    @ramveerla 2 ปีที่แล้ว

    Excellent! Straight and to the point

  • @nicolasgaritagoitia5536
    @nicolasgaritagoitia5536 3 ปีที่แล้ว

    This is the best, thank you very much. The explanations in the python official documentation is a lot harder to understand

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

    very very clear and complete explanation, thank you so much!!!

  • @CsabaKiss69
    @CsabaKiss69 6 ปีที่แล้ว

    Perfect explanation. Well done!

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

    Thanks a lot! A very informative and clear explanation!

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

    Great, You just made it simple. Thanks

  • @tymothylim6550
    @tymothylim6550 3 ปีที่แล้ว

    Thank you very much for this video! It was very interesting and I didn't know we could use "global" and "nonlocal" :)

  • @randomsamurai8759
    @randomsamurai8759 3 ปีที่แล้ว

    Just an organic comment to show some appreciation. Best python tutorials on the internet!

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

    Corey great tutorial as always. The scope of lists and dictionaries seem to behave differently in this situation -would love to have one of your vids going into that. Many thanks Adrian

  • @Roule_n_Scratche
    @Roule_n_Scratche 2 ปีที่แล้ว

    Thanks for a easy explanation on the topic

  • @raibahati
    @raibahati 3 ปีที่แล้ว

    I am gonna contribute because i have learned so much thanks to you!

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

    Too good. The explanation of these concepts

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

    Thanks a lot Corey Schafer.

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

    Thanks a lot. You explained it so beautifully.

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

    Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.

  • @anuragkadam7935
    @anuragkadam7935 2 ปีที่แล้ว

    Corey's python videos are "global" treasure

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

    You're really awesome. thank you so much,
    and please keep on doing more videos

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

    You are an outstanding teacher! Although I get most of this content now, it took me a long time to get this concept in my head. Wish I'd seen this demonstration around 25 years ago. I'd say even if the rules may be slightly different in other languages, the logic still applies.

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

    Really great tutorial!

  • @eriscrub8807
    @eriscrub8807 4 ปีที่แล้ว

    Thank you for explaining it very well. i finally understood it

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

    Awesome tutorial!!!

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

    Great tutorial. Thank you.

  • @anup2000ster
    @anup2000ster 6 ปีที่แล้ว

    Beautifully explained!!!

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

    Thank you for this video..... This video has been really helpful.....

  • @cataphrac
    @cataphrac 6 ปีที่แล้ว

    That was a thorough explanation, I grok scope now thanks!

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

    Love your videos. Thank you so much!

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

    Couldn't go without liking the video !!

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

    A perfect explanation!

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

    Really good video for understanding

  • @nnmkb
    @nnmkb 6 ปีที่แล้ว

    You are just awesome man!!! Will definitely contribute in your account.

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

    Easily understood, thank you.

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

    This is the perfect tutorial.

  • @manivannan33
    @manivannan33 2 ปีที่แล้ว

    Excellent explanation!

  • @rodrigocbv
    @rodrigocbv 4 ปีที่แล้ว

    really good tutorial! Well done!

  • @leonardopetribu7207
    @leonardopetribu7207 2 ปีที่แล้ว

    Wow, that was excellent!! Thanks!!

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

    Really useful video @corey schafer

  • @markkelton5220
    @markkelton5220 3 ปีที่แล้ว

    Great videos Corey. Hi from Australia. ;)

  • @0x656e
    @0x656e 4 ปีที่แล้ว +2

    hey Corey! if you reading this I hope you get all the happiness in this world because you deserve them thank you so much for these beautiful and great tutorials you're wonderful

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

      Thanks!

  • @tonjeandersen2429
    @tonjeandersen2429 4 ปีที่แล้ว

    This is super helpful, thank you!

  • @newsjuice7404
    @newsjuice7404 2 ปีที่แล้ว

    I learned python from you including frameworks like Flask & Django plus pandas, Numpy, mayplotlib, SQL also & I guess I am job ready now & the very first thing I ll do is to support this channel inshallah

  • @user-bx1or2rr6e
    @user-bx1or2rr6e 7 ปีที่แล้ว +1

    high quality tutorial, thanks.