1
00:00:06,680 --> 00:00:11,630
The first thing I want to talk about is a concept known in programming as Scope.

2
00:00:12,200 --> 00:00:16,190
And this scope is something that we've actually come across already in our

3
00:00:16,190 --> 00:00:20,090
programming, but I wanted to leave it until we've seen functions,

4
00:00:20,330 --> 00:00:24,230
we've seen if and while loops, different types and blocks of code,

5
00:00:24,470 --> 00:00:29,090
before we start talking about this really important concept. Now,

6
00:00:29,120 --> 00:00:30,950
if you want to visualize this concept,

7
00:00:31,400 --> 00:00:36,400
imagine you have a house with a fence around it and you plant an apple tree

8
00:00:37,100 --> 00:00:42,020
inside your garden. Think about who can access those apples.

9
00:00:42,230 --> 00:00:45,980
Just you and your family right? But what if instead,

10
00:00:46,100 --> 00:00:50,540
you had an apple tree in the neighborhood by the sidewalk, for example.

11
00:00:51,020 --> 00:00:55,640
Well, this is kind of free for all. Anybody can go and access those apples.

12
00:00:56,300 --> 00:00:59,690
This is a starting point for how to understand scope.

13
00:01:00,500 --> 00:01:04,940
Head over to the App Brewery Day 12 start Repl.it and go ahead and fork your own

14
00:01:04,940 --> 00:01:05,773
copy of it.

15
00:01:06,140 --> 00:01:11,140
Take a look at the code that's in the starting file in the main.py and try to

16
00:01:12,110 --> 00:01:14,540
understand what it's doing. First

17
00:01:14,540 --> 00:01:17,840
I create a variable called enemies and I set it to equal 1.

18
00:01:18,470 --> 00:01:21,500
Then I create a function called increase_enemies,

19
00:01:21,800 --> 00:01:25,610
and I try to set that variable to 2. Finally,

20
00:01:25,640 --> 00:01:29,630
I call my function triggering all the lines of code inside,

21
00:01:30,200 --> 00:01:35,000
and now I want you to predict what will this print statement print in the

22
00:01:35,000 --> 00:01:38,120
console and what will this print statement print?

23
00:01:38,960 --> 00:01:41,630
And it might not be what you expect it to be.

24
00:01:42,380 --> 00:01:47,270
So I would expect that here I've got a variable which is equal to one.

25
00:01:47,750 --> 00:01:49,340
When this function is called,

26
00:01:49,670 --> 00:01:53,090
this variable enemies should now be equal to two.

27
00:01:53,510 --> 00:01:56,750
So when it's printed down here from outside my function,

28
00:01:57,050 --> 00:02:01,940
this should be equal to two, and this should also be equal to two.

29
00:02:02,540 --> 00:02:04,100
But look at what happens instead.

30
00:02:05,540 --> 00:02:08,870
I get enemies from inside the function equal to 2,

31
00:02:09,410 --> 00:02:12,590
but from outside the function, it's equal to 1.

32
00:02:12,980 --> 00:02:16,460
So what's going on here? To really understand this

33
00:02:16,490 --> 00:02:20,390
we have to understand the concept of scope. And scope is a really,

34
00:02:20,390 --> 00:02:22,880
really important thing in programing.

35
00:02:22,910 --> 00:02:27,080
You see it in every programming language and although its a little bit different

36
00:02:27,080 --> 00:02:28,790
between the programming languages,

37
00:02:29,330 --> 00:02:33,950
it all goes back to that concept of the apple tree being within a fence or

38
00:02:33,980 --> 00:02:34,940
outside a fence.

39
00:02:35,360 --> 00:02:39,380
So let's take a look at some other examples before we come back to solve this

40
00:02:39,380 --> 00:02:40,213
problem.

41
00:02:41,330 --> 00:02:45,080
The first thing to think about is a concept called local scope.

42
00:02:45,590 --> 00:02:49,970
Local scope exists within functions. So for example,

43
00:02:50,150 --> 00:02:54,650
if I was creating a game and I had a function called drink_potion

44
00:02:54,680 --> 00:02:57,740
which is somehow going to strengthen my player,

45
00:02:58,220 --> 00:03:03,220
well, maybe it would have a variable that's created inside the function.

46
00:03:03,830 --> 00:03:06,820
So it could be something like potion_strength

47
00:03:08,080 --> 00:03:11,380
and I could set that to start off where the value of two.

48
00:03:12,280 --> 00:03:13,330
Now in this case

49
00:03:13,360 --> 00:03:18,360
if I was to go and print this variable potion strength from within the function,

50
00:03:19,060 --> 00:03:20,650
notice I'm indented here,

51
00:03:21,190 --> 00:03:24,910
and of course I need to call this function for that to happen.

52
00:03:26,380 --> 00:03:30,640
Notice what happens when I run the code. It's equal to two.

53
00:03:31,150 --> 00:03:32,680
That's what we would expect, right?

54
00:03:33,340 --> 00:03:38,340
But notice what happens if I try to print the potion_strength from outside the

55
00:03:40,060 --> 00:03:44,740
function here. This firstly gives me an error

56
00:03:45,010 --> 00:03:49,390
right when I'm writing it inside the editor. And when I tried to run it,

57
00:03:49,420 --> 00:03:54,420
it gives me an error inside the console. And the error is a name error

58
00:03:54,760 --> 00:03:59,760
and it tells me that the name potion strength is not defined. Clearly I've

59
00:04:00,490 --> 00:04:05,020
defined it here, but why can't I access it outside the function?

60
00:04:05,320 --> 00:04:08,320
Well, this goes back to the concept of local scope.

61
00:04:08,830 --> 00:04:13,830
When you create a new variable or indeed a new function inside another function,

62
00:04:16,690 --> 00:04:18,700
it's only accessible,

63
00:04:18,700 --> 00:04:23,700
you can only use it when you're inside that function because it has local scope.

64
00:04:25,540 --> 00:04:29,920
It's only valid within the walls of this drink_

65
00:04:29,950 --> 00:04:30,940
potion function.

66
00:04:33,040 --> 00:04:37,420
What if we wanted it to be accessible outside the function? Well,

67
00:04:37,420 --> 00:04:40,810
now we need to think about something called global scope.

68
00:04:41,440 --> 00:04:46,440
The only difference between global scope and local scope is where you define or

69
00:04:46,750 --> 00:04:49,720
where you create your variables or your functions.

70
00:04:50,380 --> 00:04:52,450
So let's say we have a different variable.

71
00:04:52,450 --> 00:04:57,190
Let's say that I created a variable called a player_health and it starts off

72
00:04:57,220 --> 00:05:02,140
being equal to 10. Now let's say later on I have the same drink_

73
00:05:02,170 --> 00:05:07,120
potion function, and this potion has strength of 2.

74
00:05:07,720 --> 00:05:11,560
So now inside my function drink_potion,

75
00:05:11,980 --> 00:05:15,550
if I wanted to print my player_health

76
00:05:15,970 --> 00:05:20,500
even though it wasn't defined within the function, this is perfectly possible.

77
00:05:20,740 --> 00:05:22,840
And if I call my function

78
00:05:22,870 --> 00:05:27,520
drink_potion and then run the code as it is,

79
00:05:27,730 --> 00:05:29,590
you will see 10 being printed.

80
00:05:30,010 --> 00:05:35,010
So this has a global scope. It's available anywhere within our file because it

81
00:05:36,250 --> 00:05:40,720
was defined at the top level of the file. Now, when I say top-level,

82
00:05:40,720 --> 00:05:43,660
I don't mean physically at the very top of the file.

83
00:05:44,020 --> 00:05:49,020
I mean that it's not within another function, like potion strength here because

84
00:05:49,930 --> 00:05:53,650
this is defined indented inside a function.

85
00:05:53,980 --> 00:05:55,960
So this is a local variable

86
00:05:55,990 --> 00:06:00,990
whereas this is a global variable. And global variables are available within

87
00:06:01,880 --> 00:06:05,210
functions, no matter how deep it gets nested

88
00:06:05,570 --> 00:06:08,120
and it's also available outside of functions.

89
00:06:09,860 --> 00:06:14,780
This concept of global and local scope doesn't just apply to variables.

90
00:06:15,140 --> 00:06:20,140
As I alluded to before, it also applies to functions and basically anything else

91
00:06:20,330 --> 00:06:24,290
you name. This is a concept called the Namespace.

92
00:06:24,890 --> 00:06:28,730
So we defined this variable player_health, we define this function

93
00:06:28,730 --> 00:06:29,563
drink_potion.

94
00:06:29,930 --> 00:06:34,930
Anything that you give a name to has a namespace and that namespace is valid in

95
00:06:37,160 --> 00:06:38,120
certain scopes.

96
00:06:39,260 --> 00:06:42,980
This concept of scope applies to basically anything you name.

97
00:06:43,940 --> 00:06:48,530
If I was to nest this function drink_potion inside another function,

98
00:06:48,560 --> 00:06:50,720
let's call it game. Well,

99
00:06:50,780 --> 00:06:55,780
this drink_potion now has local scope within the function game.

100
00:06:56,660 --> 00:07:01,660
So this is why this line is now erroring out because it can't actually see inside

101
00:07:03,620 --> 00:07:07,700
this function. So now in order to call this drink_potion,

102
00:07:08,030 --> 00:07:11,780
I have to be within the four walls of the game function.

103
00:07:12,830 --> 00:07:17,000
So whenever you give name to anything, a function or a variable,

104
00:07:17,270 --> 00:07:20,210
you have to be aware of where you created it.

105
00:07:20,780 --> 00:07:25,280
Now it's easy to notice where you've created functions, we have the def keyword,

106
00:07:25,310 --> 00:07:28,010
right? But when you create a variable

107
00:07:28,040 --> 00:07:32,570
that's the first time that you give it a name and you set it equal to something.

108
00:07:33,200 --> 00:07:37,880
And where you write that line of code defines the scope of that particular

109
00:07:37,880 --> 00:07:42,140
variable. Here it's outside of every other function,

110
00:07:42,470 --> 00:07:46,700
whereas here it's inside a function actually nested two levels deep.

111
00:07:47,090 --> 00:07:50,510
So it has a local scope to this function.

