In this tutorial we will talk about scope of variables before that.
We will discuss names bases in python using an example then we use a way to build a name in a program.
Python creates or looks up for the variable in what is known as a namespace which is a place where a
variable or a name lives and names spaces.
Containers for mapping the variable names to objects objects are the values that these variables hold.
So here we have defined a variable x.
That means we have assign assigned value tend to this variable x.
So Python creates a namespace that means it create a dictionary with this variable name and the value
can and such a mapping between the variable name and the value allows us to access this value using
these variable names.
And this mapping happens behind the scene so nothing is usually visible to us so when you try to access
this variable Python looks for this variable in the namespace that is being created.
Now let's look at another example.
So here is another example.
I have defined two functions add and then might in my program.
Now the function add has defined two parameters A and B and within the function body we are going to
add these two arguments and display the output and coming to the function might it has parameters c
and d.
It multiplies these two arguments and displays the output when we call a function its namespace is initialized
with the arguments that we call it with.
So the names are taken from the functions argument list and the objects are those the person.
So the namespace say for example let's say the namespace for the first function is at underscored namespace
and you know the elements within the namespace contained the mapping between the variable names and
the objects.
That is devalues the namespace for this function and contains the key a which is mapped to the argument
that we have Part 3.
And then it has the variable B which is mapped to the argument past which is for.
So the namespace for the function and contains
two key value pairs.
Next going to the function my let's call the namespace as might underscore namespace.
So this namespace contains the variable C and this is mapper to devalue 5 that we have parsed as an
argument and then we have the variable D which is mapped to D value 6
every function creates its own namespace and different name spaces can exist at a given time and they
are completely isolated.
These same variable name can exist in two different name spaces.
As you can see here I have now changed the parameter names to a and b in the function mind and similarly
in the namespace.
I have these two variable names because the name spaces for the function add and the namespace for the
function might add isolated the variable names can be the same.
In these two names spaces and they do not collide.
The variables that we define inside a function are called local variables and they are accessible only
within that particular function.
So the variables A and B that are defined in the function add are only accessible within this function
similarly the variables A and B which are defined in this function mode are accessible only within this
function.
Mind if you tried to access a variable which is defined within a function from outside you get an exception
Python raises an exception.
So here you have the function and which displays the sum of the variables A and B and you are passing
these two arguments making a function called and this is the function body.
So this is the lifetime of the Variable variables A and B so that means they are accessible only within
this particular function.
When you try to access to a outside function you will get an exception because Python does not recognize
this variable a outside the function let's check this in the Jupiter notebook.
So here I have defined the function and which displays the addition of the variables.
And so lets execute this piece of code.
And this is D output.
Now we will try to access the variable M outside the function definition.
We will use a print function to display this variable.
So let's execute this code.
Python has raised an exception.
Name error because the program does not recognize the variable M because it is only defined and accessible
within this function.
We have talked about local variables no apart from local variables.
We have global variables which are defined at the beginning of the script at the top of the script.
Say for example I define a variable X equal to 10 and global variables are defined outside of all functions
and at the top of the script.
And these global variables at accessible at all binds of your program including from within a function.
Here is the example I have defined the global variable x.
I have added to print statement first print statement.
Print the value of X from inside this function and add another print statement.
Print the value of x outside the function just to show that the global variables are accessible from
anywhere in your program.
So let's execute this code global variables are accessed within a function as well.
That as local variables when you tried to access them from outside the function it is defined it throws
an error whenever we make a function call a namespace is initialized which we call in other examples
at underscore namespace and the namespace contains the mapping between the variables defined in your
function and the values that we pass as arguments.
Similarly for the variables that dad declared in the global namespace that are the global variables
are different namespace is initialized for all the variables declared in the global namespace and the
local namespace is deleted either.
Then a function and when it returns out of the function raises an exception which is not dealt with
within the function.
Similarly a namespace for all the variables declared as global variables is also initialized with say
for example in our program.
Let's say the name spaces global underscored namespace.
And since we have declared only one variable that is X it contained this key and then devalue 10 and
the global namespace for a program is generated when the program is written and it lasts until the program
end or the interpreter quits and we have said that the named spaces are completely isolated from each
other.
So named spaces can have variables with the same name.
So what do we mean by that.
Say for example in my global space I declared a variable a and assign it a value 10.
So in my global namespace I'm going to have the key as a and the value 10 and this key or debateable
name in the global namespace is different from the variable name in the add namespace which was initialized
for the function.
And so they're both different even though the names are the same so we have talked about local variables
and global variables.
Now we will talk about the scope of variables using some examples I have first defined a variable B
in the global names space.
This is a global variable.
So internally Python create a global namespace let's call it global underscored namespace.
And this contains all the mapping between the name or debateable name B and the value 6.
Similarly when we make a function call to this function 1 another namespace is initialized.
Say let's say it is function underscored namespace a variable has been defined by these same variable
name as D global namespace.
But both of them are in different name spaces and this variable is assigned to the value 7.
Now let execute this code in the Jupiter notebook the example that we were discussing.
You have your variable B which is defined as a global variable defined outside of all the functions
at the top of your program and then you have your function.
Function 1 and you have use the same variable name B to define another local variable within this function.
Now let's try and execute this code and this has raised and edit the local variable B is referenced
before assignment.
So this means we can not reference a variable until it has been assigned a value.
And in order to understand this exception we need to talk about the N E G B rule.
So we know that a program can have any number of name spaces which are independent of each other and
and not accessible from anywhere in the program.
So for example a local variable is accessible only from within that program and a credible scope decide
the part of the program from where the name can be accessed based on where the name is referenced in
the program.
The name is searched in the name spaces in a specific order as body is E G B rule so here we have a
program which defines the variable x here in the global scope.
That means this variable X which is defined at the top level of your program is accessible from anywhere
in your program nixed.
We have also defined a function output and we have used a variable name X and assigned the value 7 and
this variable x is different from the variable a that has been defined outside of all the functions.
The variable X that we have defined in the outer function is accessible within this function.
And also if we have any nested functions defined in this outer function can access this variable X next.
We also have defined a nested function within the outer function called inert and we have used the same
variable name that is X in the inner function as well.
And this variable x is only accessible within this function.
So we can see that the same variable name can be used at different hierarchy levels or different scopes
and whenever we define a function in a program this will create its own namespace beginning with this
function in it.
When we define this function python is going to create a namespace for this in function.
So the variable name X will be mapped to the value 35 going to the next level.
That is the outer function we have used the same variable name X in a different scope.
And this time we are mapping the variable x to devalue 7.
Now going to the global scope this time we have mapped debatable X to a string.
Global so here we have multiple independent namespace and these variable names can be reused for different
name spaces.
Only the objects are unique so named spaces can exist independently from each other and they are structured
in a certain hierarchy.
The namespace at the global level or the global scope which is placed at the top.
And then we have the namespace defined for the outer function.
And this is the namespace for the nested function so named spaces have different levels of hierarchy.
And this brings us to the concept of scope scope in Python defined which name spaces will be looked
in and in what are the we want to print devalue the variable x using the print function.
And how does Python know which namespace it has to search if we want to print the value of this variable
the search for any reference always starts in the local scope or the local namespace and moves out outwards
until it reaches the modules global namespace before moving on to the built in modules.
We will have a detailed discussion about the search order in the upcoming sessions.
For now we understand that when our variable name is referenced in Python the interpreter searches for
it in the namespace starting from the smallest scope that is the local scope and progressively moves
outward until Python Ido find the name or raises a name Edward.
Exception e.g. B is a short form for local enclosed global in group.
Whenever Python searches for a variable name the name is searched in the local namespace.
If it is reference inside.
Function definition all the variables that are defined in function.
Searched first local refers to the local namespace and if it does not find a variable with a variable
with such name in the local namespace it next goes to the next school.
That is the enclosed scope in the enclosed to score python.
Searches for variable names that are defined in the enclosed functions are nested functions and then
if it does not find debatable name in the enclosed scope it goes to the global namespace so it searches
the top level namespace in a program.
If the variable is still not found it searches the built in scope which is nothing but deep Python built
in module so this built in scope refers to Python's built in module.
And if the name is not found in any of the Scopes L E G B exception is D.
Let's get back to our example in Jupiter notebook.
This is the example that we were discussing about earlier when we have executed this code Python has
raised this exception when Python executed this function.
The false statement is a print function.
Print the variable B debatable B is defined in the in the local namespace that is for the function.
Your function 1.
And also it is defined in the global namespace and as per the LGB rule Python searches for a variable
name in the local namespace first.
It searches for this variable B in the local namespace but we haven't defined the variable B and we
are trying to bring this variable be using different function.
So we are trying to access this variable even before defining it.
We have defined this variable in the second line.
That is the reason Python has raised an error.
No let's do one thing let's comment out this particular line and then execute the code again.
So as you can see there is no error though.
Value file that you see in the output here is from within the function.
So we have assigned devalue Phi to the local variable beat and then the value 6 that you see in the
output is from the global variable that we have defined a day top of the script.