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 equals one.
Then we create a function called confusion, where we have a equals to five 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.
One and five.
Is that what you expected?
Let's go.
Line by line.
First we assign value of one 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 going to put it in memory.
When you need to use me, just let me know.
And then it's going to go to line eight and line eight is going to say, hey, print a what's a well,
the only time that I've seen a is right here a equals one.
So go into memory, let's say A equals one.
But then when I run confusion like this.
I'm going to say, hey, a is going to equal to five and now we return A which is five.
OC 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 equals one.
Then we define the function, and then we print confusion.
Now confusion here is going to assign a equal to five and then return a.
But why didn't this change?
Why is a still equal to one 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 five.
But I'm creating a variable here called A in my own universe.
I don't know anything about a equals one 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 a 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 once 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 equals to ten.
Now in here.
We'll 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 that parent function that gets called.
So when we call that, we assign a equals to ten.
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 ten.
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.
OC does my parent local school 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 equals to ten, 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 equals to ten 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 some let's say, just give it five.
Is going to check or actually, let's not give it anything.
Let's just do some here.
When I run this.
I get built in function some.
Now.
How did they know what this was?
What this variable at the end of the day was?
Well, because it said, hey, do I have some inside of this universe?
Nope.
I don't.
Do I have some in my parent scope?
Nope, I don't.
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 some some sort of a built in Python function?
Yep.
Python actually gives us some so you can use it so it doesn't error out.
And this is the scope rules that the Python interpreter follows.
Very, very cool.
There's still a few more things that we need to learn.
So I'll see you in the next video.