1 00:00:00,270 --> 00:00:04,019 -: I wanna address a question that you may or may not have 2 00:00:04,019 --> 00:00:07,440 but it's something that's taken me years to understand 3 00:00:07,440 --> 00:00:08,430 and that is, 4 00:00:08,430 --> 00:00:12,750 why not just have everything as global variables? 5 00:00:12,750 --> 00:00:16,079 I mean, all this confusion of who has access to who. 6 00:00:16,079 --> 00:00:18,450 How easy would it be if everything was just 7 00:00:18,450 --> 00:00:20,460 on the main page, all our information, 8 00:00:20,460 --> 00:00:22,980 all the data on our global scope 9 00:00:22,980 --> 00:00:25,770 so that everything has access to everything? 10 00:00:25,770 --> 00:00:27,780 Wouldn't that be easier? 11 00:00:27,780 --> 00:00:30,990 And you'd be kind of right. 12 00:00:30,990 --> 00:00:35,019 I mean, that would make all this headache go away, right? 13 00:00:35,019 --> 00:00:37,016 But you have to remember 14 00:00:37,016 --> 00:00:41,070 that machines don't have infinite power 15 00:00:41,070 --> 00:00:44,882 don't have infinite CPU, don't have infinite memory. 16 00:00:44,882 --> 00:00:47,760 They all have limited resources. 17 00:00:47,760 --> 00:00:50,400 And as programmers, we have to be conscious 18 00:00:50,400 --> 00:00:52,620 of what resources we use 19 00:00:52,620 --> 00:00:54,990 because sometimes that can cost us money, 20 00:00:54,990 --> 00:00:57,570 sometimes that can crash our computers. 21 00:00:57,570 --> 00:01:01,140 And scope is a great demonstration of this. 22 00:01:01,140 --> 00:01:03,854 For example, this code right here, 23 00:01:03,854 --> 00:01:07,800 when this function is run, 24 00:01:07,800 --> 00:01:11,875 we're creating technically just one location 25 00:01:11,875 --> 00:01:16,440 in memory for the X variable. 26 00:01:16,440 --> 00:01:20,130 So we have that bookshelf in our computer that is X, 27 00:01:20,130 --> 00:01:22,230 that's pointing to local 28 00:01:22,230 --> 00:01:24,360 when we actually call this function. 29 00:01:24,360 --> 00:01:28,721 And then when we say non-local here, well, 30 00:01:28,721 --> 00:01:32,400 we're saying just don't create another bookshelf for us. 31 00:01:32,400 --> 00:01:34,500 Just use the one that we already have 32 00:01:34,500 --> 00:01:36,660 and assign it non-local. 33 00:01:36,660 --> 00:01:39,300 If we didn't have this line, 34 00:01:39,300 --> 00:01:42,180 by the time we get to line seven, 35 00:01:42,180 --> 00:01:45,210 we've placed in memory X equals to local 36 00:01:45,210 --> 00:01:47,220 and X equals to non-local. 37 00:01:47,220 --> 00:01:49,710 So we have two locations now in memory. 38 00:01:49,710 --> 00:01:52,470 Now this isn't a big deal because, well, 39 00:01:52,470 --> 00:01:55,500 in this day and age, we do have a lot of memory, 40 00:01:55,500 --> 00:01:57,630 but as programs get larger and larger, 41 00:01:57,630 --> 00:02:00,840 this does become a bit of a problem. 42 00:02:00,840 --> 00:02:03,990 Okay, but what about a function? 43 00:02:03,990 --> 00:02:05,970 I mean, we learned that functions allow us 44 00:02:05,970 --> 00:02:07,500 to not repeat ourselves 45 00:02:07,500 --> 00:02:10,620 and being able to call outer multiple times. 46 00:02:10,620 --> 00:02:13,547 But another good use of functions is that once 47 00:02:13,547 --> 00:02:18,360 we call this function and all of this is done, 48 00:02:18,360 --> 00:02:21,990 the computer and the Python interpreter specifically, 49 00:02:21,990 --> 00:02:24,210 destroys all this memory. 50 00:02:24,210 --> 00:02:28,200 That is, once we finish with the outer function, 51 00:02:28,200 --> 00:02:31,350 I can't really call print X here. 52 00:02:31,350 --> 00:02:32,640 It's going to give me an error. 53 00:02:32,640 --> 00:02:34,680 It's gonna say, I have no idea what X is. 54 00:02:34,680 --> 00:02:36,546 And why is that? 55 00:02:36,546 --> 00:02:41,186 It's because after we call this function, the Python, 56 00:02:41,186 --> 00:02:45,120 what we call the garbage collector, is going to say, hey, 57 00:02:45,120 --> 00:02:47,070 it looks like we're done with this function 58 00:02:47,070 --> 00:02:50,430 and I see that this X variable, well, 59 00:02:50,430 --> 00:02:53,562 and this X variable, we're not gonna use 60 00:02:53,562 --> 00:02:55,484 because we're done with this function. 61 00:02:55,484 --> 00:02:57,103 So I'm going to collect that garbage 62 00:02:57,103 --> 00:02:58,125 and then just throw it out. 63 00:02:58,125 --> 00:02:59,970 So I'm gonna empty that memory cupboard 64 00:02:59,970 --> 00:03:04,500 so that other resources or other programs can use that. 65 00:03:04,500 --> 00:03:06,720 And that is a really nice feature, 66 00:03:06,720 --> 00:03:09,750 where Python just automatically removes these for you 67 00:03:09,750 --> 00:03:12,720 so that you don't clog up the computer's memory. 68 00:03:12,720 --> 00:03:14,965 And that's why scope is useful. 69 00:03:14,965 --> 00:03:17,610 We don't have to think about it in such detail 70 00:03:17,610 --> 00:03:19,086 like I've mentioned it, but it's nice to know 71 00:03:19,086 --> 00:03:22,941 that it's there so that your programs don't hog up a lot 72 00:03:22,941 --> 00:03:25,704 of memory and they can run efficiently. 73 00:03:25,704 --> 00:03:27,780 This is a bit of an advanced topic 74 00:03:27,780 --> 00:03:29,344 but I did wanna include it in here so that you can think 75 00:03:29,344 --> 00:03:33,784 about why programs are designed the way they are. 76 00:03:33,784 --> 00:03:36,240 All right, I'm gonna stop talking now. 77 00:03:36,240 --> 00:03:37,662 I'll see you in the next video. 78 00:03:37,662 --> 00:03:38,495 Bye-bye.