WEBVTT

0
00:06.410 --> 00:11.840
The first thing I want to talk about is a concept known in programming as Scope.

1
00:11.840 --> 00:16.880
And this scope is something that we've actually come across already in our programming,

2
00:16.880 --> 00:20.120
but I wanted to leave it until we've seen functions,

3
00:20.120 --> 00:24.290
we've seen if and while loops, different types and blocks of code,

4
00:24.290 --> 00:28.280
before we start talking about this really important concept.

5
00:28.430 --> 00:35.390
Now, if you want to visualize this concept, imagine you have a house with a fence around it and you

6
00:35.390 --> 00:38.900
plant an apple tree inside your garden.

7
00:39.020 --> 00:43.910
Think about who can access those apples, just you and your family, right?

8
00:44.480 --> 00:50.720
But what if instead you had an apple tree in the neighborhood by the sidewalk, for example?

9
00:50.720 --> 00:55.880
Well, this is kind of free for all Anybody can go and access those apples.

10
00:56.030 --> 00:59.810
This is a starting point for how to understand scope.

11
01:00.440 --> 01:06.290
Take a look at the code that's in the starting file and try to understand what it's doing.

12
01:06.320 --> 01:10.670
First I create a variable called enemies and I set it to equal 1.

13
01:10.700 --> 01:17.000
Then I create a function called increase_enemies() and I try to set that variable to 2.

14
01:17.140 --> 01:22.420
Finally, I call my function triggering all the lines of code inside.

15
01:22.420 --> 01:29.320
And now I want you to predict what will this print statement print in the console and what will this

16
01:29.320 --> 01:30.910
print statement print.

17
01:31.240 --> 01:34.480
And it might not be what you expect it to be.

18
01:34.630 --> 01:42.610
So I would expect that here I've got a variable which is equal to 1 when this function is called this

19
01:42.610 --> 01:45.760
variable enemies should now be equal to 2.

20
01:45.790 --> 01:51.820
So when it's printed down here from outside my function this should be equal to 2,

21
01:51.820 --> 01:54.760
and this should also be equal to 2.

22
01:54.790 --> 02:01.630
But look at what happens instead, I get enemies from inside the function equal to 2,

23
02:01.660 --> 02:05.290
but from the outside the function it's equal to 1.

24
02:05.290 --> 02:07.180
So what's going on here?

25
02:07.300 --> 02:11.800
To really understand this, we have to understand the concept of scope.

26
02:11.800 --> 02:15.490
And scope is a really, really important thing in programming.

27
02:15.490 --> 02:20.620
You see it in every programming language, and although it's a little bit different between the programming

28
02:20.620 --> 02:27.640
languages, it all goes back to that concept of the apple tree being within a fence or outside a fence.

29
02:27.640 --> 02:32.650
So let's take a look at some other examples before we come back to solve this problem.

30
02:33.610 --> 02:37.780
The first thing to think about is a concept called Local Scope.

31
02:37.810 --> 02:41.200
Local scope exists within functions.

32
02:41.200 --> 02:48.430
So for example, if I was creating a game and I had a function called drink_potion(), which is somehow

33
02:48.430 --> 02:56.320
going to strengthen my player, well, maybe it would have a variable that's created inside the function.

34
02:56.320 --> 02:59.560
So it could be something like potion_strength,

35
03:00.280 --> 03:04.210
and I could set that to start off with a value of 2.

36
03:04.510 --> 03:11.590
Now in this case, if I was to go and print this variable potion_strength from within the function,

37
03:11.590 --> 03:13.420
notice I'm indented here.

38
03:13.420 --> 03:17.590
And of course I need to call this function for that to happen.

39
03:18.630 --> 03:21.180
Notice what happens when I run the code.

40
03:21.750 --> 03:23.400
It's equal to 2.

41
03:23.430 --> 03:25.410
That's what we would expect, right?

42
03:25.440 --> 03:34.230
But notice what happens if I try to print the potion_strength from outside the function here.

43
03:35.040 --> 03:41.910
This firstly gives me an error right when I'm writing it inside the editor, and when I try to run it,

44
03:41.940 --> 03:44.580
it gives me an error inside the console.

45
03:44.670 --> 03:47.130
And the error is a NameError.

46
03:47.130 --> 03:51.840
And it tells me that the "name 'potion_strength' is not defined."

47
03:52.080 --> 03:57.660
Clearly, I've defined it here, but why can't I access it outside the function?

48
03:57.690 --> 04:01.050
Well, this goes back to the concept of local scope.

49
04:01.050 --> 04:11.220
When you create a new variable or indeed a new function inside another function, it's only accessible,

50
04:11.220 --> 04:18.090
you can only use it when you're inside that function because it has local scope.

51
04:18.090 --> 04:23.670
It's only valid within the walls of this drink potion function.

52
04:25.320 --> 04:29.400
What if we wanted it to be accessible outside the function?

53
04:29.430 --> 04:33.660
Well, now we need to think about something called Global Scope.

54
04:33.870 --> 04:40.800
The only difference between global scope and local scope is where you define or where you create your

55
04:40.800 --> 04:42.540
variables or your functions.

56
04:42.660 --> 04:44.970
So let's say we have a different variable.

57
04:44.970 --> 04:48.300
Let's say that I created a variable called player_health,

58
04:48.420 --> 04:51.090
and it starts off being equal to 10.

59
04:51.540 --> 04:59.930
Now let's say later on I have the same drink_potion() function and this potion has strength of 2.

60
04:59.960 --> 05:09.650
So now inside my function, drink_potion(), if I wanted to print my player health even though it wasn't

61
05:09.650 --> 05:13.100
defined within the function, this is perfectly possible.

62
05:13.100 --> 05:22.310
And if I call my function drink_potion() and then run the code as it is, you'll see 10 being printed.

63
05:22.310 --> 05:25.430
So this has a global scope,

64
05:25.430 --> 05:31.550
it's available anywhere within our file because it was defined at the top level of the file.

65
05:31.550 --> 05:36.350
Now when I say top level, I don't mean physically at the very top of the file.

66
05:36.350 --> 05:44.690
I mean that it's not within another function like potion_strength() here, because this is defined indented

67
05:44.690 --> 05:46.310
inside a function.

68
05:46.310 --> 05:51.080
So this is a local variable, whereas this is a global variable.

69
05:51.080 --> 05:57.860
And global variables are available within functions no matter how deep it gets nested.

70
05:57.860 --> 06:00.770
And it's also available outside of functions.

71
06:02.120 --> 06:07.520
This concept of global and local scope doesn't just apply to variables.

72
06:07.520 --> 06:13.850
As I alluded to before, it also applies to functions and basically anything else you name.

73
06:13.880 --> 06:17.060
This is a concept called the Namespace.

74
06:17.180 --> 06:19.970
So we defined this variable player_health,

75
06:19.970 --> 06:22.250
we define this function drink_potion,

76
06:22.250 --> 06:26.330
anything that you give a name to has a namespace.

77
06:26.330 --> 06:30.800
And that namespace is valid in certain scopes.

78
06:31.580 --> 06:35.870
This concept of scope applies to basically anything you name.

79
06:36.230 --> 06:42.500
If I was to nest this function drink_potion() inside another function, let's call it game(),

80
06:42.620 --> 06:49.010
well, this drink_potion() now has local scope within the function of game().

81
06:49.010 --> 06:57.310
So this is why this line is now erroring out because it can't actually see inside this function.

82
06:57.340 --> 07:04.510
So now in order to call this drink_potion(), I have to be within the four walls of the game() function.

83
07:05.140 --> 07:12.040
So whenever you give name to anything, a function or a variable, you have to be aware of where you

84
07:12.040 --> 07:13.000
created it.

85
07:13.030 --> 07:16.540
Now it's easy to notice where you've created functions.

86
07:16.540 --> 07:18.340
We have the def keyword right?

87
07:18.340 --> 07:24.550
But when you create a variable, that's the first time that you give it a name and you set it equal

88
07:24.550 --> 07:25.450
to something.

89
07:25.450 --> 07:31.300
And where you write that line of code defines the scope of that particular variable.

90
07:31.690 --> 07:38.530
Here it's outside of every other function, whereas here it's inside a function actually nested two

91
07:38.530 --> 07:39.430
levels deep,

92
07:39.430 --> 07:43.240
so it has local scope to this function.