Instructor: All right, let's play a little game.
Can you guess what the output of this program
is going to be?
I have over here, a = 1.
Then we create a function called confusion
where we have a = 5
and we return a.
My question is,
when we print a and when we print confusion,
what do you expect to happen here?
Take a second to figure out.
Pause if you need to.
And let's find out.
1 and 5.
Is that what you expected?
Let's go line by line.
First, we assign value of 1 to variable a.
We then create a function called confusion.
And python interpreter is going to say,
all right, confusion function.
I know what it is.
I'm gonna put it in memory.
When you need to use me, just let me know.
And then it's going to go to line 8.
And line 8 is going to say,
hey, print a.
What's a?
Well, the only time that I've seen a is right here,
a = 1.
So going to memory, what's a?
a = 1.
But then when I run confusion like this,
I'm going to say,
hey, a is going to equal to 5.
And now we return a, which is 5.
Okay.
What if I change the order of this?
What if I go like this?
What will happen?
if I click run,
is that what you expected?
Once again, we have a = 1,
then we define the function,
and then we print confusion.
Now, confusion here is going to assign a equal to 5
and then return a.
But why didn't this change?
Why is a still equal to 1 by the time we get here?
And this is because of scope, right?
What the Python interpreter does
is when it runs the confusion function,
it's going to say,
hey, a is going to equal to 5
but I'm creating a variable here called a
in my own universe.
I don't know anything about a = 1 here.
I have my own universe.
And after we run this function,
then, well, we're done.
We've returned the function and it's completely gone.
But we never left the universe into the global scope.
So there's a set of rules
that the Python interpreter goes through
to check a variable.
And the rules are this.
First, it's going to check with the local.
So start with local.
And what we call is a local scope.
A local scope is a scope that's part of this local function.
So a is local scope.
It's going to check there.
Hey, do I know the variable a?
Well, yeah.
When I return a,
I have a variable defined here.
So it starts there.
But let's say a wasn't there.
Let's say I remove a
and I click run here.
I get both ones.
Because the second rule is
if there's nothing in the local variable or local scope,
then number two,
is there a parent local scope?
In our case, what's the parent of this function?
Well, it's the global scope.
But we can also do another function.
Let's say, parent.
And parent is a parent of the confusion function.
And in here, let's say a = 10.
Now in here, we will return confusion
just so we can use it again
and actually run it.
So let's call parent
and click run.
All right, let's go through this so it's not too confusing.
We have the parent function that gets called.
So when we call that,
we assign a equals to 10,
and then we also create a new function confusion.
And then finally, we return confusion and actually run it.
So we're going to return a,
and then in here,
print whatever the value of a is,
which is 10.
And why is that?
Well, because we first start with the local scope.
Hey, does confusion know what a is?
Nope, I don't have it in my local scope.
Okay, Does my parent local scope have it?
So it's going to go up a level and say,
hey, does parent function know what a is?
Yep, I have a = 10.
So it's going to print that.
The third rule or the third order is global.
And global is what we call the indentation of nothing.
It's this file, right?
Whatever the file has, that is global.
In our case, if a = 10 doesn't exist here,
there's no local, there's no parent local,
and I click run,
it checks the global scope.
And then finally, there's one other rule,
and this is number four, and it's a tricky one.
And this is what we call built-in Python
or built-in Python functions.
So Python comes with predefined functions
such as, well, the sum that we've talked about before.
This sum, let's say just give it 5,
is going to check,
or actually let's not give it anything,
let's just do sum here.
When I run this,
I get built-in function sum.
Now, how did I know what this was?
What this variable at the end of the day was?
Well, because it said,
hey, do I have sum inside of this universe?
Nope, I don't.
Okay, do I have some in my parent scope?
Nope, I don't.
Okay, do I have some in global?
Nope, there's nothing sum in here.
And then the final thing it's going to check is,
hey, is sum some sort of a built-in Python function?
Yep, Python actually gives us sum,
so you can use it.
So it doesn't error out.
And this is the scope rules
that the Python interpreter falls.
Very, very cool.
There's still a few more things that we need to learn.
So I'll see you in the next video.