‫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 B here.
‫What is this parameter when I use B like this?
‫So let's say print.
‫Be.
‫And in here I'll run confusion with, let's say, the number 300.
‫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.
‫OC 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?
‫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.
‫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 a 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 of 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 globals 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.
‫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, 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 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 to one.
‫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.
‫And 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.