WEBVTT

0
00:06.110 --> 00:13.640
Now, unlike some other programming languages, if you've come from, say, C++, or Java,

1
00:13.910 --> 00:17.420
there is no such thing as Block Scope in Python.

2
00:17.450 --> 00:26.450
What this means is that if you were to create an if statement, say if 3 > 2: and

3
00:26.450 --> 00:32.960
if you were to create a new variable inside an if block, or a while loop, or a for loop, basically

4
00:32.960 --> 00:38.270
any sort of block of code that's indented, this does not count as a fence.

5
00:38.300 --> 00:43.220
It still has the same scope as its enclosing function,

6
00:43.220 --> 00:46.760
or if there's no enclosing function, then it has global scope.

7
00:46.790 --> 00:48.980
So let me show you a full example.

8
00:49.010 --> 00:54.830
Let's say we had a list of enemies so the enemies could be ["skeletons", "zombies", "aliens"].

9
00:55.010 --> 01:01.700
So now if I was to define a game_level, right? Like the level that the user is currently playing at,

10
01:01.700 --> 01:08.630
let's say they're on Level 3 and I create an if statement and I check if the game_level is less

11
01:08.630 --> 01:10.310
than Level 5,

12
01:10.310 --> 01:16.110
well, in that case, I want to create a new_enemy, but I don't want the enemy to be too difficult to

13
01:16.140 --> 01:16.830
beat.

14
01:16.830 --> 01:22.020
So I'm going to pick from the list of enemies and I'm going to pick the first one.

15
01:22.410 --> 01:31.020
Notice how even though this new_enemy is a variable that's created within this if block if I go outside

16
01:31.020 --> 01:31.770
the if block,

17
01:31.770 --> 01:37.080
so I'm not indented at all anymore and I try to print this new_enemy,

18
01:37.680 --> 01:39.810
this is perfectly valid code.

19
01:39.810 --> 01:43.530
And if I run the code you'll see 'Skeleton' being printed.

20
01:43.740 --> 01:49.590
But notice how as soon as I embed this within a function.

21
01:49.890 --> 01:52.560
So let's define a new function.

22
01:53.250 --> 01:59.340
And now this line error is out because within the function there is local scope,

23
01:59.340 --> 02:03.900
so now this new_enemy is available anywhere within this function,

24
02:03.900 --> 02:11.400
because blocks like if, while, for, all of these blocks of code with colons and indentation, they don't

25
02:11.400 --> 02:13.590
count as creating a local scope.

26
02:13.590 --> 02:20.790
So in order to print this new_enemy, I actually have to be within the boundary of this function, which

27
02:20.790 --> 02:23.080
means my code has to be here.

28
02:23.890 --> 02:31.480
The most important thing to remember from this is if you create a variable within a function, then

29
02:31.480 --> 02:34.660
it's only available within that function.

30
02:34.780 --> 02:42.970
But if you create a variable within an if block, or a while loop, or a for loop or anything that has

31
02:42.970 --> 02:49.360
the indentation and the colon, then that does not count as creating a separate local scope.

32
02:49.450 --> 02:57.310
But one thing you might see when you're using some variable that is defined within a block, such as

33
02:57.310 --> 03:04.810
an if or a for is, you might get this warning from the linter, which reminds us how to write good

34
03:04.810 --> 03:10.690
Python code that the local variable might be referenced before assignment.

35
03:10.690 --> 03:16.960
So it's basically saying, well, what if the game_level is not less than five?

36
03:16.990 --> 03:19.810
What if it was like ten?

37
03:19.840 --> 03:26.620
Well, in this case, if we try to run this code, this print statement will never be executed because

38
03:26.620 --> 03:29.680
this if statement is not true.

39
03:29.680 --> 03:33.880
So this variable new_enemy is never created.

40
03:33.890 --> 03:36.170
So then what are we printing here?

41
03:36.170 --> 03:37.580
We're printing nothing.

42
03:37.580 --> 03:38.330
Just air.

43
03:38.360 --> 03:38.990
Right?

44
03:38.990 --> 03:46.100
So in order to get rid of this warning, what you can do is outside of any blocks, such as if, for, or

45
03:46.100 --> 03:51.710
while you can declare and create that variable and initialize it.

46
03:51.710 --> 03:55.790
So let's create new_enemy and set it to empty.

47
03:55.820 --> 04:02.030
Well, in this case, what it sees now is that there is a new_enemy variable.

48
04:02.030 --> 04:10.010
It has been already created, and depending on these conditions it might be modified, but it will always

49
04:10.040 --> 04:11.870
be able to be accessed.

50
04:11.870 --> 04:14.960
So the outcome doesn't really change.

51
04:14.960 --> 04:22.220
but the linter sees these situations where something could potentially go wrong and is just reminding

52
04:22.220 --> 04:22.610
you.

53
04:22.610 --> 04:23.990
So you have two choices.

54
04:23.990 --> 04:29.600
You can ignore it if you know what it's saying is complete rubbish because you know your code better

55
04:29.600 --> 04:38.450
than it does, or you can follow its orders and simply initialize the variable before you ever use it,

56
04:38.450 --> 04:45.170
and instead of creating the variable inside a block that may not be accessed.