‫I want to address a question that you may or may not have, but it's something that's taken me years
‫to understand, and that is why not just have everything as global variables.
‫I mean, all this confusion of who has access to who.
‫How easy would it be if everything was just on the main page?
‫All our information, all the data on our global scope so that everything has access to everything,
‫wouldn't that be easier?
‫And you'd be kind of right.
‫I mean, that would make all this headache go away, right?
‫But you have to remember that machines don't have infinite power, don't have infinite CPU, don't have
‫infinite memory.
‫They all have limited resources.
‫And as programmers, we have to be conscious of what resources we use, because sometimes that can cost
‫us money, sometimes they can crash our computers.
‫And scope is a great demonstration of this.
‫For example, this code right here, when this function is run, we're creating technically just one
‫location in memory for the X variable.
‫So we have that bookshelf in our computer that is X that's pointing to local when we actually call this
‫function.
‫And then when we say non-local here, well, we're saying just don't create another bookshelf for us.
‫Just use the one that we already have and assign it non-local.
‫If we didn't have this line, by the time we get to line seven, we've placed in memory x equals to
‫local and x equals to non-local.
‫So we have two locations now in memory.
‫Now this isn't a big deal because well, in this day and age we do have a lot of memory.
‫But as programs get larger and larger, this does become a bit of a problem.
‫OC But what about a function?
‫I mean, we learn that functions allow us to not repeat ourselves and being able to call out or multiple
‫times.
‫But another good use of functions is that once we call this function and all of this is done, the computer
‫and the python interpreter specifically destroys all this memory.
‫That is, once we finish with the outer function, I can't really call print x here.
‫It's going to give me an error.
‫It's going to say, I have no idea what X is and why is that?
‫It's because after we call this function, the python, what we call the garbage collector, is going
‫to say, hey, it looks like we're done with this function.
‫And I see that this X variable well in this X variable we're not going to use because we're done with
‫this function.
‫So I'm going to collect that garbage and then just throw it out.
‫So I'm going to empty that memory cupboard so that other resources or other programs can use that.
‫And that is a really nice feature where Python just automatically removes these for you so that you
‫don't clog up the computer's memory.
‫And that's why scope is useful.
‫We don't have to think about it in such detail like I've mentioned it, but it's nice to know that it's
‫there so that your programs don't hog up a lot of memory and they can run efficiently.
‫This is a bit of an advance topic, but I did want to include it in here so that you can think about
‫why programs are designed the way they are.
‫All right.
‫I'm going to stop talking now.
‫I'll see you in the next video.
‫Bye bye.