Instructor: 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 b.
And in here, I'll run confusion,
with let's say b 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.
Okay, what about this?
What if we have this, a,
but I wanna 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 gonna 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.
Okay, 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 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 gonna be used in here,
and then we can say total plus equals one.
If I run this,
hey 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 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 two 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 wanna 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.