1 00:00:00,120 --> 00:00:06,120 In this tutorial we will talk about scope of variables before that. 2 00:00:06,120 --> 00:00:17,250 We will discuss names bases in python using an example then we use a way to build a name in a program. 3 00:00:18,170 --> 00:00:29,360 Python creates or looks up for the variable in what is known as a namespace which is a place where a 4 00:00:29,360 --> 00:00:36,280 variable or a name lives and names spaces. 5 00:00:36,320 --> 00:00:47,120 Containers for mapping the variable names to objects objects are the values that these variables hold. 6 00:00:47,120 --> 00:00:52,640 So here we have defined a variable x. 7 00:00:52,790 --> 00:00:58,760 That means we have assign assigned value tend to this variable x. 8 00:00:58,910 --> 00:01:13,320 So Python creates a namespace that means it create a dictionary with this variable name and the value 9 00:01:13,860 --> 00:01:25,380 can and such a mapping between the variable name and the value allows us to access this value using 10 00:01:25,470 --> 00:01:26,760 these variable names. 11 00:01:27,690 --> 00:01:40,190 And this mapping happens behind the scene so nothing is usually visible to us so when you try to access 12 00:01:40,220 --> 00:01:48,430 this variable Python looks for this variable in the namespace that is being created. 13 00:01:49,470 --> 00:01:52,530 Now let's look at another example. 14 00:01:53,160 --> 00:01:55,220 So here is another example. 15 00:01:55,350 --> 00:02:01,660 I have defined two functions add and then might in my program. 16 00:02:01,920 --> 00:02:12,570 Now the function add has defined two parameters A and B and within the function body we are going to 17 00:02:12,690 --> 00:02:22,380 add these two arguments and display the output and coming to the function might it has parameters c 18 00:02:22,380 --> 00:02:23,440 and d. 19 00:02:23,460 --> 00:02:33,720 It multiplies these two arguments and displays the output when we call a function its namespace is initialized 20 00:02:33,780 --> 00:02:36,610 with the arguments that we call it with. 21 00:02:36,810 --> 00:02:44,690 So the names are taken from the functions argument list and the objects are those the person. 22 00:02:44,790 --> 00:02:55,550 So the namespace say for example let's say the namespace for the first function is at underscored namespace 23 00:02:57,580 --> 00:03:06,520 and you know the elements within the namespace contained the mapping between the variable names and 24 00:03:06,520 --> 00:03:07,590 the objects. 25 00:03:07,600 --> 00:03:19,890 That is devalues the namespace for this function and contains the key a which is mapped to the argument 26 00:03:19,890 --> 00:03:22,680 that we have Part 3. 27 00:03:22,800 --> 00:03:31,710 And then it has the variable B which is mapped to the argument past which is for. 28 00:03:31,890 --> 00:03:36,300 So the namespace for the function and contains 29 00:03:39,010 --> 00:03:41,380 two key value pairs. 30 00:03:41,770 --> 00:03:52,550 Next going to the function my let's call the namespace as might underscore namespace. 31 00:03:52,550 --> 00:04:03,770 So this namespace contains the variable C and this is mapper to devalue 5 that we have parsed as an 32 00:04:03,770 --> 00:04:13,530 argument and then we have the variable D which is mapped to D value 6 33 00:04:15,990 --> 00:04:27,720 every function creates its own namespace and different name spaces can exist at a given time and they 34 00:04:27,720 --> 00:04:29,800 are completely isolated. 35 00:04:30,220 --> 00:04:36,850 These same variable name can exist in two different name spaces. 36 00:04:37,120 --> 00:04:46,840 As you can see here I have now changed the parameter names to a and b in the function mind and similarly 37 00:04:46,840 --> 00:04:48,190 in the namespace. 38 00:04:48,190 --> 00:04:57,880 I have these two variable names because the name spaces for the function add and the namespace for the 39 00:04:57,910 --> 00:05:05,080 function might add isolated the variable names can be the same. 40 00:05:05,080 --> 00:05:09,430 In these two names spaces and they do not collide. 41 00:05:09,670 --> 00:05:21,030 The variables that we define inside a function are called local variables and they are accessible only 42 00:05:21,090 --> 00:05:24,030 within that particular function. 43 00:05:24,030 --> 00:05:34,050 So the variables A and B that are defined in the function add are only accessible within this function 44 00:05:35,200 --> 00:05:45,850 similarly the variables A and B which are defined in this function mode are accessible only within this 45 00:05:45,910 --> 00:05:46,720 function. 46 00:05:46,730 --> 00:05:56,050 Mind if you tried to access a variable which is defined within a function from outside you get an exception 47 00:05:56,140 --> 00:05:58,690 Python raises an exception. 48 00:05:58,750 --> 00:06:10,510 So here you have the function and which displays the sum of the variables A and B and you are passing 49 00:06:10,510 --> 00:06:16,950 these two arguments making a function called and this is the function body. 50 00:06:16,990 --> 00:06:27,680 So this is the lifetime of the Variable variables A and B so that means they are accessible only within 51 00:06:27,840 --> 00:06:30,030 this particular function. 52 00:06:30,030 --> 00:06:41,520 When you try to access to a outside function you will get an exception because Python does not recognize 53 00:06:41,520 --> 00:06:48,850 this variable a outside the function let's check this in the Jupiter notebook. 54 00:06:48,910 --> 00:06:55,440 So here I have defined the function and which displays the addition of the variables. 55 00:06:55,470 --> 00:06:59,300 And so lets execute this piece of code. 56 00:06:59,440 --> 00:07:01,620 And this is D output. 57 00:07:01,690 --> 00:07:08,790 Now we will try to access the variable M outside the function definition. 58 00:07:09,020 --> 00:07:14,250 We will use a print function to display this variable. 59 00:07:14,390 --> 00:07:16,780 So let's execute this code. 60 00:07:16,820 --> 00:07:19,660 Python has raised an exception. 61 00:07:19,700 --> 00:07:31,640 Name error because the program does not recognize the variable M because it is only defined and accessible 62 00:07:32,240 --> 00:07:35,080 within this function. 63 00:07:35,100 --> 00:07:39,850 We have talked about local variables no apart from local variables. 64 00:07:39,900 --> 00:07:48,040 We have global variables which are defined at the beginning of the script at the top of the script. 65 00:07:48,090 --> 00:08:00,210 Say for example I define a variable X equal to 10 and global variables are defined outside of all functions 66 00:08:00,330 --> 00:08:04,230 and at the top of the script. 67 00:08:04,230 --> 00:08:15,530 And these global variables at accessible at all binds of your program including from within a function. 68 00:08:15,570 --> 00:08:20,450 Here is the example I have defined the global variable x. 69 00:08:20,480 --> 00:08:25,340 I have added to print statement first print statement. 70 00:08:25,430 --> 00:08:32,350 Print the value of X from inside this function and add another print statement. 71 00:08:32,440 --> 00:08:42,220 Print the value of x outside the function just to show that the global variables are accessible from 72 00:08:42,400 --> 00:08:44,680 anywhere in your program. 73 00:08:44,680 --> 00:08:52,740 So let's execute this code global variables are accessed within a function as well. 74 00:08:53,880 --> 00:09:01,300 That as local variables when you tried to access them from outside the function it is defined it throws 75 00:09:01,510 --> 00:09:11,320 an error whenever we make a function call a namespace is initialized which we call in other examples 76 00:09:11,420 --> 00:09:18,380 at underscore namespace and the namespace contains the mapping between the variables defined in your 77 00:09:18,380 --> 00:09:23,320 function and the values that we pass as arguments. 78 00:09:23,420 --> 00:09:33,140 Similarly for the variables that dad declared in the global namespace that are the global variables 79 00:09:33,330 --> 00:09:43,880 are different namespace is initialized for all the variables declared in the global namespace and the 80 00:09:43,970 --> 00:09:48,220 local namespace is deleted either. 81 00:09:48,260 --> 00:09:55,820 Then a function and when it returns out of the function raises an exception which is not dealt with 82 00:09:56,240 --> 00:09:57,930 within the function. 83 00:09:57,950 --> 00:10:08,780 Similarly a namespace for all the variables declared as global variables is also initialized with say 84 00:10:08,780 --> 00:10:11,260 for example in our program. 85 00:10:11,330 --> 00:10:17,870 Let's say the name spaces global underscored namespace. 86 00:10:18,270 --> 00:10:29,560 And since we have declared only one variable that is X it contained this key and then devalue 10 and 87 00:10:29,560 --> 00:10:39,310 the global namespace for a program is generated when the program is written and it lasts until the program 88 00:10:39,400 --> 00:10:50,670 end or the interpreter quits and we have said that the named spaces are completely isolated from each 89 00:10:50,670 --> 00:10:51,570 other. 90 00:10:51,930 --> 00:10:58,060 So named spaces can have variables with the same name. 91 00:10:58,200 --> 00:10:59,690 So what do we mean by that. 92 00:10:59,730 --> 00:11:09,130 Say for example in my global space I declared a variable a and assign it a value 10. 93 00:11:09,280 --> 00:11:22,100 So in my global namespace I'm going to have the key as a and the value 10 and this key or debateable 94 00:11:22,160 --> 00:11:33,360 name in the global namespace is different from the variable name in the add namespace which was initialized 95 00:11:33,360 --> 00:11:34,380 for the function. 96 00:11:34,410 --> 00:11:45,080 And so they're both different even though the names are the same so we have talked about local variables 97 00:11:45,230 --> 00:11:47,300 and global variables. 98 00:11:47,300 --> 00:11:56,090 Now we will talk about the scope of variables using some examples I have first defined a variable B 99 00:11:56,410 --> 00:11:59,110 in the global names space. 100 00:11:59,150 --> 00:12:01,270 This is a global variable. 101 00:12:01,430 --> 00:12:11,100 So internally Python create a global namespace let's call it global underscored namespace. 102 00:12:11,360 --> 00:12:21,590 And this contains all the mapping between the name or debateable name B and the value 6. 103 00:12:22,220 --> 00:12:32,500 Similarly when we make a function call to this function 1 another namespace is initialized. 104 00:12:32,930 --> 00:12:41,600 Say let's say it is function underscored namespace a variable has been defined by these same variable 105 00:12:41,600 --> 00:12:45,170 name as D global namespace. 106 00:12:45,170 --> 00:12:54,770 But both of them are in different name spaces and this variable is assigned to the value 7. 107 00:12:54,980 --> 00:13:03,750 Now let execute this code in the Jupiter notebook the example that we were discussing. 108 00:13:03,750 --> 00:13:11,870 You have your variable B which is defined as a global variable defined outside of all the functions 109 00:13:12,200 --> 00:13:16,890 at the top of your program and then you have your function. 110 00:13:17,030 --> 00:13:28,130 Function 1 and you have use the same variable name B to define another local variable within this function. 111 00:13:28,130 --> 00:13:40,550 Now let's try and execute this code and this has raised and edit the local variable B is referenced 112 00:13:40,850 --> 00:13:42,230 before assignment. 113 00:13:42,410 --> 00:13:49,310 So this means we can not reference a variable until it has been assigned a value. 114 00:13:52,000 --> 00:14:00,480 And in order to understand this exception we need to talk about the N E G B rule. 115 00:14:01,920 --> 00:14:11,350 So we know that a program can have any number of name spaces which are independent of each other and 116 00:14:11,770 --> 00:14:15,910 and not accessible from anywhere in the program. 117 00:14:15,910 --> 00:14:26,740 So for example a local variable is accessible only from within that program and a credible scope decide 118 00:14:26,830 --> 00:14:35,080 the part of the program from where the name can be accessed based on where the name is referenced in 119 00:14:35,080 --> 00:14:36,100 the program. 120 00:14:36,190 --> 00:14:49,420 The name is searched in the name spaces in a specific order as body is E G B rule so here we have a 121 00:14:49,420 --> 00:14:57,280 program which defines the variable x here in the global scope. 122 00:14:57,280 --> 00:15:05,880 That means this variable X which is defined at the top level of your program is accessible from anywhere 123 00:15:05,950 --> 00:15:08,670 in your program nixed. 124 00:15:08,680 --> 00:15:22,070 We have also defined a function output and we have used a variable name X and assigned the value 7 and 125 00:15:22,070 --> 00:15:29,870 this variable x is different from the variable a that has been defined outside of all the functions. 126 00:15:29,870 --> 00:15:37,130 The variable X that we have defined in the outer function is accessible within this function. 127 00:15:37,990 --> 00:15:48,730 And also if we have any nested functions defined in this outer function can access this variable X next. 128 00:15:48,730 --> 00:15:57,610 We also have defined a nested function within the outer function called inert and we have used the same 129 00:15:58,120 --> 00:16:03,540 variable name that is X in the inner function as well. 130 00:16:04,120 --> 00:16:10,410 And this variable x is only accessible within this function. 131 00:16:10,750 --> 00:16:21,250 So we can see that the same variable name can be used at different hierarchy levels or different scopes 132 00:16:21,880 --> 00:16:31,680 and whenever we define a function in a program this will create its own namespace beginning with this 133 00:16:32,280 --> 00:16:33,790 function in it. 134 00:16:34,820 --> 00:16:43,780 When we define this function python is going to create a namespace for this in function. 135 00:16:43,850 --> 00:16:51,820 So the variable name X will be mapped to the value 35 going to the next level. 136 00:16:51,860 --> 00:16:59,410 That is the outer function we have used the same variable name X in a different scope. 137 00:16:59,510 --> 00:17:04,940 And this time we are mapping the variable x to devalue 7. 138 00:17:05,060 --> 00:17:15,910 Now going to the global scope this time we have mapped debatable X to a string. 139 00:17:15,910 --> 00:17:28,020 Global so here we have multiple independent namespace and these variable names can be reused for different 140 00:17:28,020 --> 00:17:29,150 name spaces. 141 00:17:29,220 --> 00:17:39,750 Only the objects are unique so named spaces can exist independently from each other and they are structured 142 00:17:39,840 --> 00:17:42,430 in a certain hierarchy. 143 00:17:42,600 --> 00:17:50,280 The namespace at the global level or the global scope which is placed at the top. 144 00:17:50,280 --> 00:17:57,530 And then we have the namespace defined for the outer function. 145 00:17:58,010 --> 00:18:08,780 And this is the namespace for the nested function so named spaces have different levels of hierarchy. 146 00:18:08,960 --> 00:18:19,390 And this brings us to the concept of scope scope in Python defined which name spaces will be looked 147 00:18:19,390 --> 00:18:29,310 in and in what are the we want to print devalue the variable x using the print function. 148 00:18:29,800 --> 00:18:37,840 And how does Python know which namespace it has to search if we want to print the value of this variable 149 00:18:39,160 --> 00:18:50,170 the search for any reference always starts in the local scope or the local namespace and moves out outwards 150 00:18:50,260 --> 00:18:58,050 until it reaches the modules global namespace before moving on to the built in modules. 151 00:18:58,120 --> 00:19:05,740 We will have a detailed discussion about the search order in the upcoming sessions. 152 00:19:05,830 --> 00:19:13,750 For now we understand that when our variable name is referenced in Python the interpreter searches for 153 00:19:13,750 --> 00:19:21,190 it in the namespace starting from the smallest scope that is the local scope and progressively moves 154 00:19:21,280 --> 00:19:27,250 outward until Python Ido find the name or raises a name Edward. 155 00:19:27,280 --> 00:19:38,570 Exception e.g. B is a short form for local enclosed global in group. 156 00:19:39,010 --> 00:19:49,450 Whenever Python searches for a variable name the name is searched in the local namespace. 157 00:19:49,500 --> 00:19:52,110 If it is reference inside. 158 00:19:52,110 --> 00:19:59,480 Function definition all the variables that are defined in function. 159 00:19:59,920 --> 00:20:11,220 Searched first local refers to the local namespace and if it does not find a variable with a variable 160 00:20:11,220 --> 00:20:18,570 with such name in the local namespace it next goes to the next school. 161 00:20:18,630 --> 00:20:24,320 That is the enclosed scope in the enclosed to score python. 162 00:20:24,370 --> 00:20:35,070 Searches for variable names that are defined in the enclosed functions are nested functions and then 163 00:20:35,340 --> 00:20:47,100 if it does not find debatable name in the enclosed scope it goes to the global namespace so it searches 164 00:20:47,520 --> 00:20:51,600 the top level namespace in a program. 165 00:20:51,600 --> 00:20:59,730 If the variable is still not found it searches the built in scope which is nothing but deep Python built 166 00:20:59,730 --> 00:21:07,980 in module so this built in scope refers to Python's built in module. 167 00:21:08,940 --> 00:21:19,450 And if the name is not found in any of the Scopes L E G B exception is D. 168 00:21:19,540 --> 00:21:23,220 Let's get back to our example in Jupiter notebook. 169 00:21:23,250 --> 00:21:28,980 This is the example that we were discussing about earlier when we have executed this code Python has 170 00:21:28,980 --> 00:21:35,290 raised this exception when Python executed this function. 171 00:21:35,320 --> 00:21:38,140 The false statement is a print function. 172 00:21:38,140 --> 00:21:48,790 Print the variable B debatable B is defined in the in the local namespace that is for the function. 173 00:21:48,790 --> 00:21:50,180 Your function 1. 174 00:21:50,320 --> 00:22:02,600 And also it is defined in the global namespace and as per the LGB rule Python searches for a variable 175 00:22:02,600 --> 00:22:05,640 name in the local namespace first. 176 00:22:05,810 --> 00:22:13,790 It searches for this variable B in the local namespace but we haven't defined the variable B and we 177 00:22:13,790 --> 00:22:18,520 are trying to bring this variable be using different function. 178 00:22:18,530 --> 00:22:22,460 So we are trying to access this variable even before defining it. 179 00:22:22,490 --> 00:22:25,280 We have defined this variable in the second line. 180 00:22:25,370 --> 00:22:29,950 That is the reason Python has raised an error. 181 00:22:30,090 --> 00:22:39,690 No let's do one thing let's comment out this particular line and then execute the code again. 182 00:22:39,720 --> 00:22:43,660 So as you can see there is no error though. 183 00:22:43,680 --> 00:22:48,710 Value file that you see in the output here is from within the function. 184 00:22:49,650 --> 00:22:56,070 So we have assigned devalue Phi to the local variable beat and then the value 6 that you see in the 185 00:22:56,070 --> 00:23:03,240 output is from the global variable that we have defined a day top of the script.