1 00:00:00,300 --> 00:00:05,880 I want to address a question that you may or may not have, but it's something that's taken me years 2 00:00:05,880 --> 00:00:12,750 to understand, and that is why not just have everything as global variables? 3 00:00:12,750 --> 00:00:18,660 I mean, all this confusion of who has access to who, how easy would it be if everything was just on 4 00:00:18,660 --> 00:00:24,720 the main page, all our information, all the data on our global scope so that everything has access 5 00:00:24,720 --> 00:00:25,590 to everything. 6 00:00:25,770 --> 00:00:27,090 Wouldn't that be easier? 7 00:00:27,690 --> 00:00:30,750 And you'd be kind of right. 8 00:00:30,960 --> 00:00:34,890 I mean, that would make all this headache go away, right? 9 00:00:35,730 --> 00:00:43,500 But you have to remember that machines don't have infinite power, don't have infinite CPU, don't have 10 00:00:43,500 --> 00:00:47,400 infinite memory, they all have limited resources. 11 00:00:47,730 --> 00:00:54,240 And as programmers, we have to be conscious of what resources we use because sometimes that can cost 12 00:00:54,240 --> 00:00:56,880 us money, sometimes that can crash our computers. 13 00:00:57,540 --> 00:01:00,590 And scope is a great demonstration of this. 14 00:01:01,110 --> 00:01:10,830 For example, this code right here, when this function is run, we're creating technically just one 15 00:01:10,830 --> 00:01:16,050 location in memory for the X variable. 16 00:01:16,440 --> 00:01:23,400 So we have that bookshelf in our computer that is X that's pointing to local when we actually call this 17 00:01:23,400 --> 00:01:23,820 function. 18 00:01:24,480 --> 00:01:32,400 And then when we say nonlocal here, well, we're saying just don't create another bookshelf for us. 19 00:01:32,400 --> 00:01:36,210 Just use the one that we already have and assign it nonlocal. 20 00:01:36,660 --> 00:01:44,790 If we didn't have this line, by the time we get to line seven, we've placed in memory X equals to 21 00:01:44,790 --> 00:01:47,010 local and X equals to nonlocal. 22 00:01:47,220 --> 00:01:49,360 So we have two locations now in memory. 23 00:01:49,720 --> 00:01:55,200 Now, this isn't a big deal because, well, in this day and age, we do have a lot of memory. 24 00:01:55,470 --> 00:02:00,060 But as programs get larger and larger, this does become a bit of a problem. 25 00:02:00,820 --> 00:02:03,980 OK, but what about a function? 26 00:02:03,990 --> 00:02:09,930 I mean, we learned that functions allow us to not repeat ourselves and being able to call out are multiple 27 00:02:09,930 --> 00:02:10,410 times. 28 00:02:10,620 --> 00:02:18,600 But another good use of functions is that once we called this function and all of this is done, the 29 00:02:18,600 --> 00:02:23,850 computer and the Python interpreter specifically destroys all this memory. 30 00:02:24,240 --> 00:02:31,110 That is, once we finish with the outer function, I can't really call print X here. 31 00:02:31,320 --> 00:02:32,490 It's going to give me an error. 32 00:02:32,670 --> 00:02:34,530 It's going to say I have no idea what exists. 33 00:02:34,650 --> 00:02:36,270 And why is that? 34 00:02:37,240 --> 00:02:44,020 It's because after we call this function, the python will be called the garbage collector is going 35 00:02:44,020 --> 00:02:47,070 to say, hey, it looks like we're done with this function. 36 00:02:47,080 --> 00:02:53,440 And I see that this X variable well, and this X variable we're not going to use because we're done 37 00:02:53,440 --> 00:02:54,060 with this function. 38 00:02:54,070 --> 00:02:57,220 So I'm going to collect that garbage and then just throw it out. 39 00:02:57,220 --> 00:03:03,970 So I'm going to empty that memory cupboard so that other resources or other programs can use that. 40 00:03:04,510 --> 00:03:10,630 And that is a really nice feature where Python just automatically removes these for you so that you 41 00:03:10,630 --> 00:03:12,340 don't clog up the computer's memory. 42 00:03:12,700 --> 00:03:14,890 And that's why SCOP is useful. 43 00:03:15,370 --> 00:03:20,290 We don't have to think about it in such detail like I've mentioned it, but it's nice to know that it's 44 00:03:20,290 --> 00:03:25,350 there so that your programs don't hog up a lot of memory and they can run efficiently. 45 00:03:26,050 --> 00:03:31,150 This is a bit of an advanced topic, but I did want to include it in here so that you can think about 46 00:03:31,270 --> 00:03:33,670 why programs are designed the way they are. 47 00:03:34,450 --> 00:03:34,870 All right. 48 00:03:34,990 --> 00:03:36,200 I'm going to stop talking now. 49 00:03:36,250 --> 00:03:37,660 I'll see you in the next video. 50 00:03:37,990 --> 00:03:38,320 Bye bye.