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 B 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't 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 a.
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 1.
So we're going to add every single time when we add count it's going to increment zero by 1 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 count
let's see what happens.
I'm going to hit run and I get an error 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 account 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 1.
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
total 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 while 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 count 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 2 1 and then we do count 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 non local.
But for that one let's take a break and I'll see you in the next video.