Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,420 --> 00:00:04,860
Oh boy there's some code on the screen and it looks terribly confusing.
2
00:00:04,860 --> 00:00:11,100
This is an example of a really convoluted complicated function that you would never write but it'll
3
00:00:11,130 --> 00:00:13,310
be good for exercise purposes.
4
00:00:13,440 --> 00:00:19,150
And we're talking specifically about this right here called non local.
5
00:00:19,230 --> 00:00:22,730
It's actually a new keyword in Python 3.
6
00:00:22,920 --> 00:00:29,370
And as you can see my ripple doesn't even notice it so it doesn't even highlighted in blue because it
7
00:00:29,370 --> 00:00:30,440
is a new feature.
8
00:00:30,570 --> 00:00:37,070
But I do want to talk to you about it and let you know what it does the non local keyword is used to
9
00:00:37,070 --> 00:00:39,650
refer to this part.
10
00:00:39,650 --> 00:00:46,880
This parent local it's a way for us to say hey I want to use a variable that is not a global variable
11
00:00:47,030 --> 00:00:54,890
but is outside of the scope of my function so based on that definition try and pause the video and see
12
00:00:54,950 --> 00:00:57,390
what this function might do.
13
00:00:57,800 --> 00:01:04,550
I'll run it right now and then go through the code if I click Run.
14
00:01:04,600 --> 00:01:10,700
This is what we get so let's talk about this.
15
00:01:10,800 --> 00:01:15,170
We have an outer function and then we call the outer function.
16
00:01:15,180 --> 00:01:22,320
So the Python interpreter is going to go and say All right we have an x variable that is local to the
17
00:01:22,590 --> 00:01:28,020
outer function and then inside of here we're going to define an inner function.
18
00:01:28,020 --> 00:01:34,290
Another function and then here before we even call that we jumped in line nine and say All right call
19
00:01:34,290 --> 00:01:35,110
inter function.
20
00:01:35,280 --> 00:01:41,560
We come back and we say hey line five I want to use non local x.
21
00:01:41,790 --> 00:01:42,690
What does that mean.
22
00:01:42,690 --> 00:01:47,910
Well I want to use this x variable because it's non local.
23
00:01:47,920 --> 00:01:51,310
That is I don't want to create a new x variable.
24
00:01:51,400 --> 00:01:59,980
I want to jump up the scope to my parents scope my parent local and grab whatever you find in there
25
00:02:00,430 --> 00:02:02,170
as long as it's not global variable.
26
00:02:02,170 --> 00:02:03,560
Any parent will do.
27
00:02:03,670 --> 00:02:05,050
I want to grab that non-local.
28
00:02:05,050 --> 00:02:09,480
So now this ex is referring to the outer function here.
29
00:02:09,550 --> 00:02:21,360
So when we say X equals non-local we're assigning this new string and replacing this local so that when
30
00:02:21,360 --> 00:02:31,020
we print inner x we get non-local but also when we print the outer X we've modified this outer scope
31
00:02:31,020 --> 00:02:32,580
with the non-local keyword.
32
00:02:32,610 --> 00:02:35,760
So that becomes non local as well.
33
00:02:35,760 --> 00:02:41,430
If I remove this line and let's comment it out and I click Run you see the difference.
34
00:02:41,430 --> 00:02:48,920
Now we have the inner non-local but then the outer local hasn't been modified because this is a new
35
00:02:49,100 --> 00:02:51,060
variable.
36
00:02:51,200 --> 00:02:58,360
Again I argue that this actually makes your code more complicated than it needs to be.
37
00:02:58,370 --> 00:03:04,160
So there are special cases where you might want to use this but if you can try to make your code predictable
38
00:03:04,220 --> 00:03:11,300
where you can avoid using things like non-local and global mind you they are there for a reason because
39
00:03:11,300 --> 00:03:14,390
they are useful in some situation.
40
00:03:14,390 --> 00:03:19,910
With that said keep in mind make your code predictable make your code clean and I'll see you in the
41
00:03:19,910 --> 00:03:20,420
next video.
4163
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.