1 00:00:06,410 --> 00:00:09,200 Coming back to our original example. 2 00:00:09,860 --> 00:00:14,780 I want to talk more about the global scope and the concept of modifying 3 00:00:14,930 --> 00:00:19,610 something within the global scope. Here we have enemies, 4 00:00:20,030 --> 00:00:22,310 which is a variable that has global scope, 5 00:00:23,030 --> 00:00:27,620 and here we have a function which creates a local scope. 6 00:00:28,370 --> 00:00:33,370 Now we think that we're tapping into this variable and setting it to two. 7 00:00:34,370 --> 00:00:37,880 But in fact, we're actually creating a completely new variable 8 00:00:37,910 --> 00:00:40,400 that's entirely separate from this one. 9 00:00:41,420 --> 00:00:44,270 And this is why when we printed it here, 10 00:00:44,510 --> 00:00:49,100 it showed it was equal to 2. But when we printed it here, it showed 11 00:00:49,130 --> 00:00:53,750 it was actually equal to 1. If you want to make this a little more obvious, 12 00:00:53,780 --> 00:00:58,780 I can call this a skeleton and I can set this to be a zombie. 13 00:01:00,590 --> 00:01:04,879 And when I hit run, you can see again, this prints zombie, 14 00:01:05,030 --> 00:01:06,680 this prints skeleton, 15 00:01:06,980 --> 00:01:11,750 because these two variables are actually entirely different things. 16 00:01:12,290 --> 00:01:15,860 And we're not changing this right here at all, 17 00:01:16,040 --> 00:01:19,640 we're just creating a new variable that has a local scope. 18 00:01:21,020 --> 00:01:26,020 Now it's usually a terrible idea to call your local variables and your global 19 00:01:26,690 --> 00:01:31,040 variables the same name. But in this case, 20 00:01:31,070 --> 00:01:36,070 what we actually wanted to do was we wanted to modify this variable, 21 00:01:36,320 --> 00:01:40,790 we wanted to do something like maybe += 1. And notice how 22 00:01:40,790 --> 00:01:42,860 as soon as I write += 1, 23 00:01:43,160 --> 00:01:48,160 that my editor starts going crazy and tells me that this local variable enemies 24 00:01:49,370 --> 00:01:53,810 is defined in an enclosing scope is referenced before assignment. 25 00:01:53,810 --> 00:01:54,643 What does that mean? 26 00:01:54,680 --> 00:01:59,540 It means that the editor thinks you're trying to tap into a local variable that 27 00:01:59,540 --> 00:02:01,700 you defined somewhere around here. 28 00:02:02,930 --> 00:02:07,400 And then you tried to modify it by adding one to the previous value, 29 00:02:07,670 --> 00:02:11,960 but you actually haven't defined it. What we wanted to do though, 30 00:02:11,960 --> 00:02:16,850 is we wanted to tap into this variable and change it here. 31 00:02:18,320 --> 00:02:19,550 In order to do this, 32 00:02:19,610 --> 00:02:24,610 we actually have to explicitly say that we have a global variable 33 00:02:25,430 --> 00:02:30,260 which is called enemies that's defined somewhere outside of this function. 34 00:02:30,740 --> 00:02:34,880 And that is the enemies that we want to use inside this function. 35 00:02:35,480 --> 00:02:40,480 So it basically takes that global enemies into the function and allows you to 36 00:02:40,700 --> 00:02:43,580 modify it. Without this line of code 37 00:02:43,700 --> 00:02:48,700 we cannot modify something that is global within a local scope. 38 00:02:50,930 --> 00:02:55,930 Now there's a reason why it's so difficult to modify something that has global 39 00:02:56,180 --> 00:02:57,013 scope. 40 00:02:57,080 --> 00:03:02,080 You probably don't actually want to do this very often, because it's confusing and 41 00:03:03,430 --> 00:03:08,430 it's prone to creating bugs and errors. Because this variable with global scope 42 00:03:09,220 --> 00:03:12,640 could have been created anywhere in your code, right? 43 00:03:12,970 --> 00:03:17,970 And you would be modifying it completely independent of when you created it. 44 00:03:18,400 --> 00:03:22,480 So it might've been days between when you wrote this code and when you wrote 45 00:03:22,480 --> 00:03:26,560 this code and it just makes everything more fallible, 46 00:03:26,590 --> 00:03:27,820 more easy to fail. 47 00:03:28,270 --> 00:03:33,270 This is why very often people will tell you when they're teaching you Python to 48 00:03:33,460 --> 00:03:37,750 avoid modifying global scope. You can read it, 49 00:03:37,780 --> 00:03:42,160 that's not a problem. You can use it within your code, like we are here. 50 00:03:43,540 --> 00:03:48,540 But don't try to modify it within a function that has local scope. 51 00:03:49,360 --> 00:03:50,680 But what can you do instead? 52 00:03:50,710 --> 00:03:55,330 What if you wanted to have this functionality like a function that changes the 53 00:03:55,330 --> 00:03:56,350 number of enemies? 54 00:03:56,860 --> 00:04:01,480 How can you achieve this without modifying the global scope within the function? 55 00:04:02,260 --> 00:04:06,940 Well, you could use what we learned about return statements instead, 56 00:04:07,060 --> 00:04:11,440 right? What if instead of modifying the enemies, 57 00:04:12,010 --> 00:04:15,310 you actually just simply returned it as the output. 58 00:04:15,700 --> 00:04:19,329 So return the current value of enemies + 1. 59 00:04:20,140 --> 00:04:22,150 Now once you call this function, 60 00:04:22,420 --> 00:04:27,280 you'll get hold of the output and you can save it to the global variable enemies. 61 00:04:27,790 --> 00:04:32,140 So this now means that this function can be taken away and placed anywhere in 62 00:04:32,140 --> 00:04:35,410 your code and you don't actually need to know how it works. 63 00:04:35,830 --> 00:04:39,010 As long as you know that this is how you increase enemies, 64 00:04:39,580 --> 00:04:42,160 then all you have to do is just call it.