Welcome back.
Let's try and answer some questions that you may have, for example, what about parameters?
If I do parameter, let's say be here, what is this parameter when I use B like this?
So let's say print.
B and in here, I'll run confusion with, let's say, the number three hundred if I run this.
This works.
And that is because be the parameter is part of the local scope, that is it's part of this.
So parameters are considered local variables.
We're able to use it inside of the function, but we can use it outside of those functions.
Technically, when we define the function, we let the interpreter know, hey, B is going to be a local
variable.
OK, what about this?
What if we have this, eh?
But I want to make sure inside of my function that I'm actually referring to the global A is there a
way for us to just use this value without creating a new variable?
Well, let's use a better example for this.
Let's say I wanted to create a counter.
So we'll have total equals to zero.
And then in here.
I'm going to say define count.
And this function is going to say total.
Plus equals one.
So we're going to add every single time when we add count, it's going to increment zero by one and
then one by one, so on and so forth.
So we're going to return total at the end of this.
OK, so if I run here, print.
Let's see what happens.
I'm going to hit run.
And I get another local variable total referenced before assignment, and that is because, well, Count
doesn't know about total.
You're trying to use total, but we haven't assigned anything yet.
But we want the total from the outside world to run, because if I just do, total equals zero here
and I run count.
That's great, I get one, but what if I wanted to run count multiple times?
What if I wanted to run count?
Let's say three times so that the count total will be three.
So let's do print on the last one here.
And I click run.
I still get one because every time we run the function, we reset the total to zero.
That's not very useful, is it?
So one way that we can fix this is using what we call the global keyword in Python and global says use
the global total if it exists in here, so that instead of having to create a new variable, I can use
the global variable total.
Check this out.
Well, I get an invalid syntax, and that is because we first have to say global total is going to be
used in here and then we can say total plus equals one if I run this.
A look at that, we have a proper counter, so global is a way for us to access this global variable.
However, I argue that this is actually not a good way of doing things because it can get really confusing
when you start adding Global's and all these different universes are accessing each other's variables,
a better way of doing this is something called dependency injection, and this is a simplified version
of it.
But the idea is that instead of accessing variables outside of the function like this, which can get
really, really complicated as files get bigger and bigger, is to do.
Instead this.
Like this, we create a parameter and then we pass in that parameter.
Or argument in here.
But as you can see, it's still one because by the time we print the third total, well, this never
changes.
This is a global zero.
So instead, we can do something like this, we can say count total, of which we're going to count
again.
And then count again.
If I run this.
I get three and I know what you're thinking, this is completely insane, it looks confusing.
Look at all these brackets.
But let me show you what we've actually done.
We're able to detach the dependency or the effect that this can't function had on the outside global
scope and instead just focus on its health.
All we needed to do was say, hey, I want you to give a count with total of zero.
And then after that, this is going to evaluate two one.
And then we do count a total of one plus one is going to equal to two and then count two plus one is
going to equal to three.
In this way, we're able to still do our count without having to use that global keyword, which I would
argue is nicer.
Mind you, if this is your first time seeing this, it can get a little bit tricky.
But at least this way, you know that there's different ways of doing things.
One other word I want to show you is something called nonlocal.
But for that one, let's take a break and I'll see you in the next video.