WEBVTT

0
00:06.140 --> 00:13.850
Coming back to our original example, I want to talk more about the Global Scope, and the concept of

1
00:13.850 --> 00:16.940
modifying something within the global scope.

2
00:17.360 --> 00:22.520
Here we have enemies, which is a variable that has global scope,

3
00:22.610 --> 00:27.860
and here we have a function which creates a local scope.

4
00:28.040 --> 00:33.930
Now we think that we're tapping into this variable and setting it to 2,

5
00:34.020 --> 00:40.590
but in fact, we're actually creating a completely new variable that's entirely separate from this one.

6
00:41.040 --> 00:46.530
And this is why when we printed it here, it showed it was equal to 2,

7
00:46.530 --> 00:51.150
but when we printed it here, it showed it was actually equal to 1.

8
00:51.390 --> 00:55.760
If you want to make this a little bit more obvious, I can call this a "Skeleton",

9
00:55.760 --> 00:59.210
and I can set this to be a "Zombie".

10
01:00.200 --> 01:04.910
And when I hit Run, you can see again, this prints 'Zombie',

11
01:04.910 --> 01:06.740
this prints 'Skeleton'.

12
01:06.740 --> 01:11.930
Because these two variables are actually entirely different things.

13
01:11.930 --> 01:15.890
And we're not changing this right here at all,

14
01:15.890 --> 01:19.790
we're just creating a new variable that has a local scope.

15
01:20.760 --> 01:29.130
Now, it's usually a terrible idea to call your local variables and your global variables the same name.

16
01:29.130 --> 01:36.270
But in this case, what we actually wanted to do was we wanted to modify this variable.

17
01:36.270 --> 01:39.810
We wanted to do something like maybe + = 1.

18
01:39.810 --> 01:46.430
And notice how as soon as I write + = 1, then my editor starts going crazy and tells me that

19
01:46.430 --> 01:53.750
this local variable 'enemies' is defined in an enclosing scope is referenced before assignment.

20
01:53.750 --> 01:54.590
What does that mean?

21
01:54.590 --> 02:00.890
It means that the editor thinks you're trying to tap into a local variable that you defined somewhere

22
02:00.890 --> 02:08.570
around here, and then you try to modify it by adding one to the previous value, but you actually haven't

23
02:08.570 --> 02:09.470
defined it.

24
02:10.100 --> 02:17.070
What we wanted to do, though, is we wanted to tap into this variable and change it here.

25
02:18.000 --> 02:26.130
In order to do this, we actually have to explicitly say that we have a global variable which is called

26
02:26.130 --> 02:33.120
enemies, that's defined somewhere outside of this function, and that is the enemies that we want to

27
02:33.120 --> 02:35.130
use inside this function.

28
02:35.130 --> 02:41.740
So it basically takes that global enemies into the function and allows you to modify it.

29
02:41.740 --> 02:49.090
Without this line of code, we cannot modify something that is global within a local scope.

30
02:50.680 --> 02:56.770
Now, there's a reason why it's so difficult to modify something that has global scope.

31
02:56.770 --> 03:04.860
You probably don't actually want to do this very often because it's confusing and it's prone to creating

32
03:04.860 --> 03:12.240
bugs and errors, because this variable with global scope could have been created anywhere in your code,

33
03:12.240 --> 03:12.690
right?

34
03:12.690 --> 03:18.150
And you would be modifying it completely independent of when you created it.

35
03:18.150 --> 03:23.400
So it might have been days between when you wrote this code and when you wrote this code,

36
03:23.400 --> 03:28.040
and it just makes everything more fallible, more easy to fail.

37
03:28.040 --> 03:35.750
This is why very often people will tell you when they're teaching you Python to avoid modifying global

38
03:35.750 --> 03:36.410
scope.

39
03:36.410 --> 03:37.730
You can read it,

40
03:37.730 --> 03:38.780
that's not a problem,

41
03:38.780 --> 03:47.900
you can use it within your code like we are here but don't try to modify it within a function that

42
03:47.900 --> 03:49.070
has local scope.

43
03:49.070 --> 03:50.690
But what can you do instead?

44
03:50.690 --> 03:56.610
What if you wanted to have this functionality like a function that changes the number of enemies?

45
03:56.640 --> 04:01.770
How can you achieve this without modifying the global scope within the function?

46
04:01.920 --> 04:07.620
Well, you could use what we learned about return statements instead, right?

47
04:07.740 --> 04:15.420
What if instead of modifying the enemies, you actually just simply returned it as the output?

48
04:15.420 --> 04:19.540
So return the current value of enemies + 1.

49
04:19.780 --> 04:26.020
Now, once you call this function, you'll get hold of the outputs and you can save it to the global

50
04:26.020 --> 04:27.460
variable enemies.

51
04:27.460 --> 04:33.550
So this now means that this function can be taken away and placed anywhere in your code, and you don't

52
04:33.550 --> 04:39.940
actually need to know how it works, as long as you know that this is how you increase enemies, then

53
04:39.940 --> 04:42.370
all you have to do is just call it.