1 00:00:00,330 --> 00:00:04,360 Oh, boy, there's some code on the screen and it looks terribly confusing. 2 00:00:04,800 --> 00:00:11,160 This is an example of a really convoluted, complicated function that you would never write, but it'll 3 00:00:11,160 --> 00:00:12,900 be good for exercise purposes. 4 00:00:13,320 --> 00:00:18,870 And we're talking specifically about this right here called nonlocal. 5 00:00:19,140 --> 00:00:22,520 It's actually a new keyword in Python three. 6 00:00:22,800 --> 00:00:27,210 And as you can see, my Reppel doesn't even notice it. 7 00:00:27,210 --> 00:00:30,210 So it doesn't even highlighted in blue because it is a new feature. 8 00:00:30,570 --> 00:00:33,390 But I do want to talk to you about it and let you know what it does. 9 00:00:34,330 --> 00:00:40,960 The nonlocal key keyword is used to refer to this part, this parent local. 10 00:00:41,200 --> 00:00:48,310 It's a way for us to say, hey, I want to use a variable that is not a global variable, but is outside 11 00:00:48,310 --> 00:00:50,380 of the scope of my function. 12 00:00:51,440 --> 00:00:58,460 So based on that definition, try and pause the video and see what this function might do, I'll run 13 00:00:58,460 --> 00:01:03,170 it right now and then go through the code if I click run. 14 00:01:04,560 --> 00:01:06,030 This is what we get. 15 00:01:07,880 --> 00:01:09,320 So let's talk about this. 16 00:01:10,750 --> 00:01:15,050 We have an outer function and then we call the outer function. 17 00:01:15,100 --> 00:01:22,000 So the Python interpreter is going to go and say, all right, we have an X variable that is local to 18 00:01:22,000 --> 00:01:23,980 the outer function. 19 00:01:24,610 --> 00:01:28,870 And then inside of here, we going to define an inner function, another function. 20 00:01:29,320 --> 00:01:34,930 And in here, before we even call that, we jumped in line nine and say, all right, call inner function. 21 00:01:35,230 --> 00:01:40,390 We come back and we say, hey, line five, I want to use nonlocal X. 22 00:01:41,300 --> 00:01:41,450 Hmm. 23 00:01:41,740 --> 00:01:42,470 What does that mean? 24 00:01:42,610 --> 00:01:47,890 Well, I want to use this X variable because it's nonlocal. 25 00:01:47,890 --> 00:01:51,160 That is, I don't want to create a new X variable. 26 00:01:51,310 --> 00:02:00,010 I want to jump up the scope to my parents, scope my parent local and grab whatever you find in there. 27 00:02:00,340 --> 00:02:03,310 As long as it's not global variable any parent will do. 28 00:02:03,550 --> 00:02:04,980 I want to grab that nonlocal. 29 00:02:04,990 --> 00:02:08,720 So now this X is referring to the outer function here. 30 00:02:09,490 --> 00:02:17,260 So when we say X equals nonlocal, we're assigning this new string. 31 00:02:18,940 --> 00:02:26,290 And replacing this local so that when we print interacts, we get nonlocal, but also when we print 32 00:02:26,290 --> 00:02:32,470 the outer X, we've modified this outer scope with the nonlocal keyword. 33 00:02:32,530 --> 00:02:34,980 So that becomes nonlocal as well. 34 00:02:35,650 --> 00:02:39,310 If I remove this line and let's comment it out and I click run. 35 00:02:40,490 --> 00:02:47,540 You see the difference now we have the inner nonlocal, but then the outer local hasn't been modified 36 00:02:47,540 --> 00:02:49,580 because this is a new variable. 37 00:02:51,090 --> 00:02:57,450 Again, I argue that this actually makes your code more complicated than it needs to be. 38 00:02:58,320 --> 00:03:04,200 So there are special cases where you might want to use this, but if you can try to make your code predictable, 39 00:03:04,200 --> 00:03:11,310 where you can avoid using things like nonlocal and global, mind you, they are there for a reason because 40 00:03:11,310 --> 00:03:13,830 they are useful in some situations. 41 00:03:14,310 --> 00:03:19,680 With that said, keep in mind, make your code predictable, make your code clean, and I'll see you 42 00:03:19,680 --> 00:03:20,430 in the next video.