1 00:00:00,510 --> 00:00:01,350 Instructor: Welcome back. 2 00:00:01,350 --> 00:00:04,230 Let's try and answer some questions that you may have. 3 00:00:04,230 --> 00:00:07,470 For example, what about parameters? 4 00:00:07,470 --> 00:00:10,770 If I do parameter, let's say b here, 5 00:00:10,770 --> 00:00:15,330 what is this parameter when I use b like this? 6 00:00:15,330 --> 00:00:19,140 So let's say print b. 7 00:00:19,140 --> 00:00:22,170 And in here, I'll run confusion, 8 00:00:22,170 --> 00:00:26,280 with let's say b number 300. 9 00:00:26,280 --> 00:00:27,753 If I run this, 10 00:00:28,830 --> 00:00:30,363 this works. 11 00:00:31,680 --> 00:00:34,950 And that is because b, the parameter, 12 00:00:34,950 --> 00:00:38,250 is part of the local scope. 13 00:00:38,250 --> 00:00:40,590 That is, it's part of this. 14 00:00:40,590 --> 00:00:44,070 So parameters are considered local variables. 15 00:00:44,070 --> 00:00:46,920 We're able to use it inside of the function, 16 00:00:46,920 --> 00:00:49,563 but we can't use it outside of those functions. 17 00:00:50,760 --> 00:00:53,250 Technically, when we define the function 18 00:00:53,250 --> 00:00:54,960 we let the interpreter know, 19 00:00:54,960 --> 00:00:58,500 hey b is going to be a local variable. 20 00:00:58,500 --> 00:01:00,180 Okay, what about this? 21 00:01:00,180 --> 00:01:04,980 What if we have this, a, 22 00:01:04,980 --> 00:01:07,290 but I wanna make sure inside of my function 23 00:01:07,290 --> 00:01:10,080 that I'm actually referring to the global a. 24 00:01:10,080 --> 00:01:13,710 Is there a way for us to just use this value 25 00:01:13,710 --> 00:01:16,890 without creating a new variable? 26 00:01:16,890 --> 00:01:19,410 Well, let's use a better example for this. 27 00:01:19,410 --> 00:01:22,920 Let's say I wanted to create a counter. 28 00:01:22,920 --> 00:01:25,443 So we'll have total equals to zero, 29 00:01:26,529 --> 00:01:28,390 and then in here 30 00:01:29,520 --> 00:01:32,223 I'm going to say define count. 31 00:01:33,240 --> 00:01:36,693 And this function is going to say total, 32 00:01:37,950 --> 00:01:39,240 plus equals one, 33 00:01:39,240 --> 00:01:41,700 so we're gonna add every single time. 34 00:01:41,700 --> 00:01:42,660 When we add count 35 00:01:42,660 --> 00:01:44,940 it's going to increment zero by one 36 00:01:44,940 --> 00:01:47,283 and then one by one, so on and so forth. 37 00:01:48,420 --> 00:01:49,620 So we're going to return 38 00:01:50,490 --> 00:01:52,800 total at the end of this. 39 00:01:52,800 --> 00:01:55,263 Okay, so if I run here, print, 40 00:01:57,180 --> 00:01:58,013 count, 41 00:02:00,390 --> 00:02:01,830 let's see what happens. 42 00:02:01,830 --> 00:02:05,910 I'm going to hit run and I get an error. 43 00:02:05,910 --> 00:02:07,680 Local variable total, 44 00:02:07,680 --> 00:02:10,560 referenced before assignment 45 00:02:10,560 --> 00:02:13,920 and that is because well count doesn't know about total. 46 00:02:13,920 --> 00:02:15,150 You're trying to use total 47 00:02:15,150 --> 00:02:18,210 but we haven't assigned anything yet. 48 00:02:18,210 --> 00:02:22,350 But we want the total from the outside world to run 49 00:02:22,350 --> 00:02:26,880 because if I just do total equals zero here 50 00:02:26,880 --> 00:02:28,443 and I run count, 51 00:02:29,670 --> 00:02:30,900 that's great, I get one. 52 00:02:30,900 --> 00:02:34,800 But what if I wanted to run count multiple times? 53 00:02:34,800 --> 00:02:36,630 What if I wanted to run count 54 00:02:36,630 --> 00:02:38,430 let's say three times 55 00:02:38,430 --> 00:02:43,020 so that the count total will be three. 56 00:02:43,020 --> 00:02:45,213 So let's do print on the last one here, 57 00:02:48,240 --> 00:02:49,233 and I click run. 58 00:02:50,730 --> 00:02:54,030 I still get one because every time we run the function 59 00:02:54,030 --> 00:02:56,340 we reset the total to zero. 60 00:02:56,340 --> 00:02:57,993 That's not very useful, is it? 61 00:02:59,460 --> 00:03:02,010 So one way that we can fix this 62 00:03:02,010 --> 00:03:07,010 is using what we call the global keyword in Python. 63 00:03:07,260 --> 00:03:08,790 And global says, 64 00:03:08,790 --> 00:03:11,520 use the global total, 65 00:03:11,520 --> 00:03:13,710 if it exists in here. 66 00:03:13,710 --> 00:03:16,530 So that instead of having to create a new variable 67 00:03:16,530 --> 00:03:20,040 I can use the global variable total. 68 00:03:20,040 --> 00:03:20,873 Check this out. 69 00:03:23,550 --> 00:03:25,380 Well, I get an invalid syntax, 70 00:03:25,380 --> 00:03:28,290 and that is because we first have to say 71 00:03:28,290 --> 00:03:30,900 global total is gonna be used in here, 72 00:03:30,900 --> 00:03:34,530 and then we can say total plus equals one. 73 00:03:34,530 --> 00:03:35,553 If I run this, 74 00:03:37,410 --> 00:03:40,770 hey look at that, we have a proper counter. 75 00:03:40,770 --> 00:03:44,433 So global is a way for us to access this global variable. 76 00:03:45,480 --> 00:03:48,360 However, I argue that this is actually 77 00:03:48,360 --> 00:03:50,520 not a good way of doing things 78 00:03:50,520 --> 00:03:52,710 because it can get really confusing 79 00:03:52,710 --> 00:03:54,480 when you start adding globals, 80 00:03:54,480 --> 00:03:57,450 and all these different universes 81 00:03:57,450 --> 00:04:00,360 are accessing each other's variables. 82 00:04:00,360 --> 00:04:02,610 A better way of doing this 83 00:04:02,610 --> 00:04:05,100 is something called dependency injection. 84 00:04:05,100 --> 00:04:07,080 And this is a simplified version of it. 85 00:04:07,080 --> 00:04:10,800 But the idea is that instead of accessing variables 86 00:04:10,800 --> 00:04:12,780 outside of the function like this, 87 00:04:12,780 --> 00:04:15,180 which can get really really complicated as files 88 00:04:15,180 --> 00:04:16,440 get bigger and bigger, 89 00:04:16,440 --> 00:04:19,533 is to do instead this, 90 00:04:21,450 --> 00:04:23,670 total like this. 91 00:04:23,670 --> 00:04:27,030 We create a parameter and then we pass in 92 00:04:27,030 --> 00:04:30,783 that parameter or argument in here. 93 00:04:31,620 --> 00:04:33,510 But as you can see, it's still one 94 00:04:33,510 --> 00:04:36,990 because by the time we print the third total 95 00:04:36,990 --> 00:04:38,340 well this never changes. 96 00:04:38,340 --> 00:04:39,813 This is a global zero. 97 00:04:41,100 --> 00:04:45,120 So instead we can do something like this. 98 00:04:45,120 --> 00:04:47,490 We can say count total, 99 00:04:47,490 --> 00:04:49,893 of which we're going to count again, 100 00:04:52,320 --> 00:04:54,723 and then count again. 101 00:04:57,450 --> 00:04:59,463 If I run this, 102 00:05:00,960 --> 00:05:03,750 I get three, and I know what you're thinking. 103 00:05:03,750 --> 00:05:05,700 This is completely insane. 104 00:05:05,700 --> 00:05:06,570 It looks confusing. 105 00:05:06,570 --> 00:05:08,010 Look at all these brackets, 106 00:05:08,010 --> 00:05:11,760 but let me show you what we've actually done. 107 00:05:11,760 --> 00:05:16,440 We're able to detach the dependency 108 00:05:16,440 --> 00:05:19,860 or the effect that this count function had 109 00:05:19,860 --> 00:05:22,710 on the outside global scope, 110 00:05:22,710 --> 00:05:25,470 and instead just focus on its health. 111 00:05:25,470 --> 00:05:26,790 All we needed to do was say, 112 00:05:26,790 --> 00:05:31,050 hey, I want you to give a count with total of zero. 113 00:05:31,050 --> 00:05:34,960 And then after that this is going to evaluate two one 114 00:05:36,090 --> 00:05:39,510 and then we do count total of one plus one 115 00:05:39,510 --> 00:05:41,550 is going to equal to two, 116 00:05:41,550 --> 00:05:46,550 and then count two plus one is going to equal to three. 117 00:05:46,590 --> 00:05:49,380 And this way we're able to still do our count 118 00:05:49,380 --> 00:05:52,980 without having to use that global keyword 119 00:05:52,980 --> 00:05:54,960 which I would argue is nicer. 120 00:05:54,960 --> 00:05:57,780 Mind you, if this is your first time seeing this, 121 00:05:57,780 --> 00:06:00,120 it can get a little bit tricky, 122 00:06:00,120 --> 00:06:01,772 but at least this way you know that there's 123 00:06:01,772 --> 00:06:04,680 different ways of doing things. 124 00:06:04,680 --> 00:06:06,930 One other word I wanna show you 125 00:06:06,930 --> 00:06:09,720 is something called non-local. 126 00:06:09,720 --> 00:06:11,400 But for that one, let's take a break 127 00:06:11,400 --> 00:06:13,150 and I'll see you in the next video.