1 00:00:00,480 --> 00:00:01,050 Welcome back. 2 00:00:01,380 --> 00:00:06,780 Let's try and answer some questions that you may have, for example, what about parameters? 3 00:00:07,440 --> 00:00:14,670 If I do parameter, let's say be here, what is this parameter when I use B like this? 4 00:00:15,330 --> 00:00:16,740 So let's say print. 5 00:00:17,830 --> 00:00:27,550 B and in here, I'll run confusion with, let's say, the number three hundred if I run this. 6 00:00:28,850 --> 00:00:30,200 This works. 7 00:00:31,640 --> 00:00:39,880 And that is because be the parameter is part of the local scope, that is it's part of this. 8 00:00:40,580 --> 00:00:44,060 So parameters are considered local variables. 9 00:00:44,060 --> 00:00:49,430 We're able to use it inside of the function, but we can use it outside of those functions. 10 00:00:50,760 --> 00:00:57,030 Technically, when we define the function, we let the interpreter know, hey, B is going to be a local 11 00:00:57,030 --> 00:00:57,570 variable. 12 00:00:58,500 --> 00:00:59,690 OK, what about this? 13 00:01:00,150 --> 00:01:04,190 What if we have this, eh? 14 00:01:04,980 --> 00:01:10,920 But I want to make sure inside of my function that I'm actually referring to the global A is there a 15 00:01:10,920 --> 00:01:16,090 way for us to just use this value without creating a new variable? 16 00:01:16,860 --> 00:01:18,690 Well, let's use a better example for this. 17 00:01:19,410 --> 00:01:21,960 Let's say I wanted to create a counter. 18 00:01:22,960 --> 00:01:25,300 So we'll have total equals to zero. 19 00:01:27,140 --> 00:01:28,250 And then in here. 20 00:01:29,460 --> 00:01:32,100 I'm going to say define count. 21 00:01:33,200 --> 00:01:36,500 And this function is going to say total. 22 00:01:37,970 --> 00:01:39,270 Plus equals one. 23 00:01:39,290 --> 00:01:45,140 So we're going to add every single time when we add count, it's going to increment zero by one and 24 00:01:45,140 --> 00:01:47,180 then one by one, so on and so forth. 25 00:01:48,450 --> 00:01:51,930 So we're going to return total at the end of this. 26 00:01:52,830 --> 00:01:55,110 OK, so if I run here, print. 27 00:02:00,400 --> 00:02:01,280 Let's see what happens. 28 00:02:01,780 --> 00:02:02,920 I'm going to hit run. 29 00:02:04,520 --> 00:02:12,830 And I get another local variable total referenced before assignment, and that is because, well, Count 30 00:02:12,830 --> 00:02:13,970 doesn't know about total. 31 00:02:13,970 --> 00:02:17,450 You're trying to use total, but we haven't assigned anything yet. 32 00:02:18,200 --> 00:02:26,000 But we want the total from the outside world to run, because if I just do, total equals zero here 33 00:02:26,840 --> 00:02:28,310 and I run count. 34 00:02:29,670 --> 00:02:34,240 That's great, I get one, but what if I wanted to run count multiple times? 35 00:02:34,800 --> 00:02:36,480 What if I wanted to run count? 36 00:02:36,660 --> 00:02:42,320 Let's say three times so that the count total will be three. 37 00:02:43,020 --> 00:02:45,090 So let's do print on the last one here. 38 00:02:48,220 --> 00:02:49,060 And I click run. 39 00:02:50,730 --> 00:02:56,070 I still get one because every time we run the function, we reset the total to zero. 40 00:02:56,340 --> 00:02:57,890 That's not very useful, is it? 41 00:02:59,450 --> 00:03:09,380 So one way that we can fix this is using what we call the global keyword in Python and global says use 42 00:03:09,380 --> 00:03:17,270 the global total if it exists in here, so that instead of having to create a new variable, I can use 43 00:03:17,270 --> 00:03:19,490 the global variable total. 44 00:03:20,060 --> 00:03:20,710 Check this out. 45 00:03:23,530 --> 00:03:29,740 Well, I get an invalid syntax, and that is because we first have to say global total is going to be 46 00:03:29,740 --> 00:03:35,380 used in here and then we can say total plus equals one if I run this. 47 00:03:37,360 --> 00:03:44,320 A look at that, we have a proper counter, so global is a way for us to access this global variable. 48 00:03:45,530 --> 00:03:52,730 However, I argue that this is actually not a good way of doing things because it can get really confusing 49 00:03:52,730 --> 00:03:59,690 when you start adding Global's and all these different universes are accessing each other's variables, 50 00:04:00,290 --> 00:04:06,650 a better way of doing this is something called dependency injection, and this is a simplified version 51 00:04:06,650 --> 00:04:06,890 of it. 52 00:04:07,070 --> 00:04:13,340 But the idea is that instead of accessing variables outside of the function like this, which can get 53 00:04:13,340 --> 00:04:17,180 really, really complicated as files get bigger and bigger, is to do. 54 00:04:17,330 --> 00:04:19,340 Instead this. 55 00:04:22,800 --> 00:04:28,290 Like this, we create a parameter and then we pass in that parameter. 56 00:04:29,470 --> 00:04:30,720 Or argument in here. 57 00:04:31,600 --> 00:04:37,870 But as you can see, it's still one because by the time we print the third total, well, this never 58 00:04:37,870 --> 00:04:38,350 changes. 59 00:04:38,380 --> 00:04:39,730 This is a global zero. 60 00:04:41,110 --> 00:04:49,210 So instead, we can do something like this, we can say count total, of which we're going to count 61 00:04:49,230 --> 00:04:49,690 again. 62 00:04:52,290 --> 00:04:54,570 And then count again. 63 00:04:57,440 --> 00:04:59,270 If I run this. 64 00:05:00,910 --> 00:05:06,590 I get three and I know what you're thinking, this is completely insane, it looks confusing. 65 00:05:06,610 --> 00:05:07,720 Look at all these brackets. 66 00:05:07,990 --> 00:05:11,020 But let me show you what we've actually done. 67 00:05:11,740 --> 00:05:21,700 We're able to detach the dependency or the effect that this can't function had on the outside global 68 00:05:22,000 --> 00:05:24,990 scope and instead just focus on its health. 69 00:05:25,450 --> 00:05:30,670 All we needed to do was say, hey, I want you to give a count with total of zero. 70 00:05:31,030 --> 00:05:34,900 And then after that, this is going to evaluate two one. 71 00:05:36,060 --> 00:05:44,220 And then we do count a total of one plus one is going to equal to two and then count two plus one is 72 00:05:44,220 --> 00:05:46,070 going to equal to three. 73 00:05:46,560 --> 00:05:53,610 In this way, we're able to still do our count without having to use that global keyword, which I would 74 00:05:53,610 --> 00:05:54,630 argue is nicer. 75 00:05:54,960 --> 00:05:59,490 Mind you, if this is your first time seeing this, it can get a little bit tricky. 76 00:06:00,090 --> 00:06:03,900 But at least this way, you know that there's different ways of doing things. 77 00:06:04,800 --> 00:06:09,210 One other word I want to show you is something called nonlocal. 78 00:06:09,690 --> 00:06:12,810 But for that one, let's take a break and I'll see you in the next video.