-: I wanna 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 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 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.
Okay, but what about a function?
I mean, we learned that functions allow us
to not repeat ourselves
and being able to call outer 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 gonna 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,
and 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 gonna 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 advanced topic
but I did wanna include it in here so that you can think
about why programs are designed the way they are.
All right, I'm gonna stop talking now.
I'll see you in the next video.
Bye-bye.