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 CPR you 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 that 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 two local and x equals two 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.
OK.
But what about a function.
I mean we learned that functions allow us to not repeat ourselves and being able to call out our 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 will be called 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 gonna 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 let's stop talking now.
I'll see you in the next video by.