1 00:00:00,330 --> 00:00:01,163 Man: Oh boy. 2 00:00:01,163 --> 00:00:02,340 There's some code on the screen 3 00:00:02,340 --> 00:00:04,800 and it looks terribly confusing. 4 00:00:04,800 --> 00:00:07,530 This is an example of a really convoluted, 5 00:00:07,530 --> 00:00:10,770 complicated function that you would never write, 6 00:00:10,770 --> 00:00:13,350 but it'll be good for exercise purposes. 7 00:00:13,350 --> 00:00:15,750 And we're talking specifically about 8 00:00:15,750 --> 00:00:19,170 this right here called non-local. 9 00:00:19,170 --> 00:00:22,860 It's actually a new keyword in Python 3. 10 00:00:22,860 --> 00:00:27,210 And as you can see, my REPL doesn't even notice it 11 00:00:27,210 --> 00:00:28,800 so it doesn't even highlight it in blue 12 00:00:28,800 --> 00:00:30,570 because it is a new feature. 13 00:00:30,570 --> 00:00:32,100 But I do want to talk to you about it 14 00:00:32,100 --> 00:00:34,350 and let you know what it does. 15 00:00:34,350 --> 00:00:38,160 The non-local keyword is used to refer 16 00:00:38,160 --> 00:00:41,220 to this part, this parent local. 17 00:00:41,220 --> 00:00:42,337 It's a way for us to say, 18 00:00:42,337 --> 00:00:47,040 "Hey, I wanna use a variable that is not a global variable 19 00:00:47,040 --> 00:00:50,577 but is outside of the scope of my function." 20 00:00:51,450 --> 00:00:53,190 So based on that definition, 21 00:00:53,190 --> 00:00:57,720 try and pause the video and see what this function might do. 22 00:00:57,720 --> 00:01:02,310 I'll run it right now and then go through the code. 23 00:01:02,310 --> 00:01:03,333 If I click run, 24 00:01:04,560 --> 00:01:06,183 this is what we get. 25 00:01:07,890 --> 00:01:09,483 So let's talk about this. 26 00:01:10,710 --> 00:01:13,200 We have an outer function 27 00:01:13,200 --> 00:01:15,120 and then we call the outer function. 28 00:01:15,120 --> 00:01:17,317 So the Python interpreter is going to go and say, 29 00:01:17,317 --> 00:01:21,780 "All right, we have an X variable that is local 30 00:01:21,780 --> 00:01:24,630 to the outer function." 31 00:01:24,630 --> 00:01:26,910 And then inside of here we're going to define 32 00:01:26,910 --> 00:01:29,370 an inner function, another function. 33 00:01:29,370 --> 00:01:33,030 And in here, before we even call that, we jump to line nine, 34 00:01:33,030 --> 00:01:35,220 and say, "All right, call inner function." 35 00:01:35,220 --> 00:01:37,830 We come back and we say, "Hey, line five, 36 00:01:37,830 --> 00:01:41,340 I wanna use non-local X." 37 00:01:41,340 --> 00:01:42,600 Hmm, what does that mean? 38 00:01:42,600 --> 00:01:46,170 Well, I wanna use this X variable, 39 00:01:46,170 --> 00:01:47,880 because it's non-local. 40 00:01:47,880 --> 00:01:51,300 That is, I don't wanna create a new X variable. 41 00:01:51,300 --> 00:01:55,680 I wanna jump up the scope to my parent scope, 42 00:01:55,680 --> 00:02:00,360 my parent local, and grab whatever you find in there. 43 00:02:00,360 --> 00:02:03,570 As long as it's not global variable, any parent will do. 44 00:02:03,570 --> 00:02:04,980 I wanna grab that non-local. 45 00:02:04,980 --> 00:02:09,449 So now this X is referring to the outer function here. 46 00:02:09,449 --> 00:02:13,290 So when we say X equals non-local, 47 00:02:13,290 --> 00:02:16,470 we're assigning this new 48 00:02:16,470 --> 00:02:20,850 string and replacing this local. 49 00:02:20,850 --> 00:02:24,810 So that when we print inner X, we get non-local, 50 00:02:24,810 --> 00:02:28,470 but also when we print the outer X, 51 00:02:28,470 --> 00:02:32,550 we've modified this outer scope with the non-local keyword, 52 00:02:32,550 --> 00:02:35,670 so that becomes non-local as well. 53 00:02:35,670 --> 00:02:38,430 If I remove this line and let's comment it out, 54 00:02:38,430 --> 00:02:41,970 and I click run, you see the difference now. 55 00:02:41,970 --> 00:02:45,900 We have the inner non-local, but then the outer 56 00:02:45,900 --> 00:02:49,713 local hasn't been modified because this is a new variable. 57 00:02:51,120 --> 00:02:55,410 Again, I argue that this actually makes your code 58 00:02:55,410 --> 00:02:58,320 more complicated than it needs to be. 59 00:02:58,320 --> 00:03:01,410 So there are special cases where you might want to use this 60 00:03:01,410 --> 00:03:04,410 but if you can, try to make your code predictable where 61 00:03:04,410 --> 00:03:08,790 you can avoid using things like non-local and global. 62 00:03:08,790 --> 00:03:12,210 Mind you, they are there for a reason because they are 63 00:03:12,210 --> 00:03:14,310 useful in some situation. 64 00:03:14,310 --> 00:03:17,550 With that said, keep in mind, make your code predictable, 65 00:03:17,550 --> 00:03:20,553 make your code clean, and I'll see you in the next video.