1
00:00:05,360 --> 00:00:10,260
so continuing on from the last video so
global keyword tells Python to

2
00:00:10,260 --> 00:00:15,140
look for a variable in the global scope
which is the modules name space but the

3
00:00:15,140 --> 00:00:19,820
non local keyword tells it to look
for that variable in the enclosing

4
00:00:19,820 --> 00:00:23,980
scopes and you could see what we've done
differently to other videos is

5
00:00:23,980 --> 00:00:28,410
we've got an embedded function here that we
didn't have at the blackjack program so

6
00:00:28,410 --> 00:00:33,540
that's why on line 7 the global keyword wasn't appropriate so again the non local

7
00:00:33,540 --> 00:00:37,830
keyword if that is telling Python to
look for the variable in the enclosing

8
00:00:37,830 --> 00:00:42,090
scope so non local won't check the
global name space though so a

9
00:00:42,090 --> 00:00:47,650
non local variable must exist in the enclosing
scope but not be global otherwise if it

10
00:00:47,650 --> 00:00:52,540
was global of course you would use the keyword
global to do that another thing to know

11
00:00:52,540 --> 00:00:56,940
about the non local variables is that they
must exist in the enclosing scope

12
00:00:56,940 --> 00:01:03,070
they can't be created in other words by the non local keywords so if we change this to try and put a declaration

13
00:01:03,070 --> 00:01:08,910
in their we get an error...end of statement expected
because it's looking for us just to define

14
00:01:08,910 --> 00:01:13,260
the variable to say that it's a non-local
variable but it has to actually exist as

15
00:01:13,260 --> 00:01:18,790
I mentioned in the outer scope or in the
enclosing scope to be more precise now

16
00:01:18,790 --> 00:01:21,860
although we didn't do this we could have
created our global variables for

17
00:01:21,860 --> 00:01:24,680
blackjack in the new_game method and they would have been

18
00:01:24,680 --> 00:01:29,380
available in the main program but it's
not considered good style to that and

19
00:01:29,380 --> 00:01:33,510
can lead to difficulty debugging the programs
but it's there and it is actually

20
00:01:33,510 --> 00:01:38,560
allowed but the same is not true for non local variables and that's because Python

21
00:01:38,560 --> 00:01:42,340
doesn't know which enclosing scope the should have existed and has really has no

22
00:01:42,340 --> 00:01:45,860
way to know whether to create them so
they must already be present in an

23
00:01:45,860 --> 00:01:51,000
enclosing scope or you get an error for that reason so at this point its a

24
00:01:51,000 --> 00:01:55,180
good time to point out that the only
things that create scope in Python are

25
00:01:55,180 --> 00:02:00,520
modules functions and classes now I've already
talked about variables in a module

26
00:02:00,520 --> 00:02:05,080
being defined in a while for loop and
then and remaining available to the rest

27
00:02:05,080 --> 00:02:09,560
of the program but that's because they
become part of the global name space now

28
00:02:09,560 --> 00:02:13,660
that can seem odd if you come from Java
for example but the idea that functions

29
00:02:13,660 --> 00:02:16,630
introduced a new scope will be familiar

30
00:02:16,630 --> 00:02:20,660
know when you define a function Python creates a new name space for it and

31
00:02:20,660 --> 00:02:26,920
any variables are part of the local scope and to that end we actually printed out the local

32
00:02:26,920 --> 00:02:32,870
variables using the locals function in
the blackjack game a few videos ago so non

33
00:02:32,870 --> 00:02:37,340
local tells python to look for
variables in an enclosing function and

34
00:02:37,340 --> 00:02:41,000
if it doesn't find it there to check in a
functioning closing that and so on

35
00:02:41,000 --> 00:02:45,210
it will not though check the global name space
as I mentioned so if you remember

36
00:02:45,210 --> 00:02:50,230
that it doesn't include globals then non local is a good name for it variables that are not

37
00:02:50,230 --> 00:02:54,130
local to the current scope ok so
hopefully that made a lot of sense

38
00:02:54,130 --> 00:02:59,790
earlier implied that functions can be
nested inside functions and themselves

39
00:02:59,790 --> 00:03:03,970
are themselves nested in another
function its quite rare to have

40
00:03:03,970 --> 00:03:07,280
more than one level of nesting in any
code that did so would have to be quite

41
00:03:07,280 --> 00:03:12,550
complicated so I use a variation of the
spam function in the tutorial to

42
00:03:12,550 --> 00:03:17,570
demonstrate a deeper level of nesting
and bearing in mind its probably not

43
00:03:17,570 --> 00:03:20,950
something you have to do very often but
the functions would be very similar and

44
00:03:20,950 --> 00:03:23,220
just returned different amounts of spam

45
00:03:23,220 --> 00:03:27,340
in this case but that will let us focus on
the scope issues rather than getting

46
00:03:27,340 --> 00:03:31,300
bogged down in complicated code so we are
gonna create a new Python file called

47
00:03:31,300 --> 00:03:41,170
spam.py and we are going to enter the
following code in it and just do a test

48
00:03:41,170 --> 00:03:50,950
on this and have a look at these functions so...

49
00:03:50,950 --> 00:03:56,730
....

50
00:04:01,040 --> 00:04:33,270
....

51
00:04:34,410 --> 00:04:40,410
....

52
00:04:41,150 --> 00:04:54,630
.....so as you can see

53
00:04:54,630 --> 00:04:59,640
here the function spam one contains
another function called spam 2 and

54
00:04:59,640 --> 00:05:08,570
that's spam 2 function contains a third
function called spam 3 and if we run this

55
00:05:08,570 --> 00:05:14,940
you can see  that spam 1 calls the spam 2 function and then adds its
result on to spam 1's text and

56
00:05:14,940 --> 00:05:18,960
spam 2 in turn adds the result of the spam
3 function on its text and

57
00:05:18,960 --> 00:05:22,730
subsequently we get a lot of spam
now to get a clearer idea of what's

58
00:05:22,730 --> 00:05:26,290
happening here we can print out the locals
dictionary from inside each of the

59
00:05:26,290 --> 00:05:29,690
functions I'm going to use a format
string so that we can add a label

60
00:05:29,690 --> 00:05:33,150
before each dictionary to be sure where
it came from so let's go ahead and do

61
00:05:33,150 --> 00:05:50,160
that i'm gonna start in spam 3....

62
00:05:50,160 --> 00:06:09,110
....

63
00:06:09,650 --> 00:06:20,160
....

64
00:06:20,160 --> 00:06:26,990
so if we run this again now I've done the same thing in my haste here

65
00:06:26,990 --> 00:06:31,940
so that should have been tabbed over
my bad their when I was adding the new lines so

66
00:06:31,940 --> 00:06:40,270
try that again now we can see now and you can see that

67
00:06:40,270 --> 00:06:44,130
it's showing which names are local to each function and we can see that for example

68
00:06:44,130 --> 00:06:49,840
the function spam 3 just contains
the string variable z but spam 2

69
00:06:49,840 --> 00:06:57,290
contains the string variable called y
you can see it here nearly off the screen but it also contains

70
00:06:57,290 --> 00:06:59,180
a function as you can see there

71
00:06:59,180 --> 00:07:05,910
called spam 3 you can see that reference their and also spam 1 has got a local variable X

72
00:07:05,910 --> 00:07:11,460
and a function called spam 2 as you can
see here so within spam 1

73
00:07:11,460 --> 00:07:16,000
itself we can't refer to the
variables y&z because variables in an

74
00:07:16,000 --> 00:07:21,650
inner scope aren't available to the outer scope but however inside spam 2 we can

75
00:07:21,650 --> 00:07:25,570
refer to x and inside spam 3 we can
refer to both X&Y

76
00:07:26,540 --> 00:07:31,410
so an inner scope has access to the variables defined in an outer scope and we can use the

77
00:07:31,410 --> 00:07:37,910
outer variables and spam 2 spam 3 lets do that by making small change to the code so

78
00:07:37,910 --> 00:07:49,460
close it down lets have a look on what its gonna look like so if we change this now and I'll make that even....

79
00:07:49,460 --> 00:08:08,930
....

80
00:08:08,930 --> 00:08:12,280
and run this to make sure it works so you could see that is working ok

81
00:08:13,000 --> 00:08:18,330
and because we're not assigning to X&Y in
the inner functions the names don't shadow

82
00:08:18,330 --> 00:08:22,330
the variable in the outer scope and as a
result the values are then available for

83
00:08:22,330 --> 00:08:27,420
use now I'm gonna demonstrate scope and
it's not necessary sensible thing

84
00:08:27,420 --> 00:08:30,950
to do here but it will demonstrate what
we're trying to get across to you and

85
00:08:30,950 --> 00:08:35,659
the reason is...

86
00:08:35,659 --> 00:08:37,600
lines 16 and 17

87
00:08:37,600 --> 00:08:42,409
cam be combined into a single expression
and when I change them lets have a look

88
00:08:42,409 --> 00:08:47,500
and see what happens so this here I'm gonna make...

89
00:08:49,800 --> 00:08:55,410
now the mini challenge here is before actually
running this program can you work at

90
00:08:55,410 --> 00:08:59,450
what's going to happen and why its going
to happen because I only done a simple thing

91
00:08:59,450 --> 00:09:04,940
as you can see I've merged lines 16 and 17 to make it
one line but I want you to see if you can

92
00:09:04,940 --> 00:09:10,540
figure out what's going to happen and
why so pause the video now and have a think about it and when

93
00:09:10,540 --> 00:09:13,540
your ready to continue come back and we'll go
through it

94
00:09:17,860 --> 00:09:22,070
ok so hopefully you have a look at it
you might be actually curious if you didn't

95
00:09:22,070 --> 00:09:27,640
figure it out so let's talk about what
actually happens now the change I made was

96
00:09:27,640 --> 00:09:31,320
quite simple and it might appear at
face value to be a simple optimization

97
00:09:31,320 --> 00:09:36,570
but in actual fact that simple
optimization breaks the code and without

98
00:09:36,570 --> 00:09:40,890
starting the spam 2 function it wasn't obvious that the code was gonna break

99
00:09:40,890 --> 00:09:45,780
so lets run it first to confirm that there
is a problem and you can see we get

100
00:09:45,780 --> 00:09:50,190
these weird errors so three variable X is
referenced before assignment in closing

101
00:09:50,190 --> 00:09:51,130
scope

102
00:09:51,130 --> 00:09:54,540
we got errors here on line 2016 and 11

103
00:09:54,540 --> 00:10:00,400
so what actually happened is that spam
2 the function attempts to use the

104
00:10:00,400 --> 00:10:06,340
value of x but spent spam 2 is called during the
initial assignment to X so x doesn't

105
00:10:06,340 --> 00:10:12,730
yet have a value as a result we get the
error free variable X

106
00:10:12,730 --> 00:10:16,070
this down here on the bottom
free variable x referenced before

107
00:10:16,070 --> 00:10:20,100
assignment in closing scope so theirs
actually 3 lessons to learn from the

108
00:10:20,100 --> 00:10:26,960
simple change I'm gonna move the it their so we can look at the source code at the same time while we are talking about it so

109
00:10:26,960 --> 00:10:31,530
the three things are wherever possible
try to write functions so that they only

110
00:10:31,530 --> 00:10:37,300
use local variables and parameters only
access global and non-local variables

111
00:10:37,300 --> 00:10:42,170
when it's absolutely necessary if you
can see an unexpected code such as our

112
00:10:42,170 --> 00:10:50,250
use of 2 lines to assign x and even also to assign y on line 11 and 12 think carefully before

113
00:10:50,250 --> 00:10:53,590
changing them because it could be that
the programmer who wrote them was having a

114
00:10:53,590 --> 00:10:58,830
bad day but also their could be a very
valid reason why they used 2 lines

115
00:10:58,830 --> 00:11:03,310
when one would appear to do so no matter
how trivial a change do you think you've

116
00:11:03,310 --> 00:11:08,110
made test the program thoroughly to make
sure you haven't broken anything that's

117
00:11:08,110 --> 00:11:11,690
often simple changes like that that will
actually break code and major changes

118
00:11:11,690 --> 00:11:15,450
will probably get through testing anyway
but very simple modifications can get

119
00:11:15,450 --> 00:11:20,100
overlooked and this will be a case of
the change that I've made on the 16 and 17 to

120
00:11:20,100 --> 00:11:22,830
merge them into one line would be
something with that would often

121
00:11:22,830 --> 00:11:29,000
get overlooked and actually there's a fourth
lesson here as well and that is that if your write unexpected

122
00:11:29,000 --> 00:11:34,250
code such as our use of 2 lines we assigning X&Y make sure you include a

123
00:11:34,250 --> 00:11:38,780
comment to explain why so the next
person who works on your code doesn't do

124
00:11:38,780 --> 00:11:45,820
what you've just done so what we do is undo the change put it back to how it was

125
00:11:45,820 --> 00:11:54,150
before and we come back and say....

126
00:11:55,070 --> 00:12:04,070
.....and on lines 11 and 12 we would

127
00:12:04,070 --> 00:12:09,480
say something like....

128
00:12:13,110 --> 00:12:20,920
...you will actually be surprised how often that next person is

129
00:12:20,920 --> 00:12:25,080
you so coming back to the code perhaps
have looked at the program you have written for a

130
00:12:25,080 --> 00:12:29,990
number of days weeks months or even years so the person that you are writing

131
00:12:29,990 --> 00:12:36,870
this comments for could ultimately be used and I just put in an extra space here should have bee watching the

132
00:12:36,870 --> 00:12:43,250
warnings to keep the compiler happy so yeah the person could actually be you

133
00:12:43,250 --> 00:12:47,490
that you're trying to help out here and you can quickly really forget why you did something

134
00:12:47,490 --> 00:12:51,320
in a certain way so using comments like
this to explain anything you do that's

135
00:12:51,320 --> 00:12:55,450
not obvious is very very highly
recommended now before I finish this

136
00:12:55,450 --> 00:12:58,820
section out theirs two more things to look
at the first is very simple

137
00:12:58,820 --> 00:13:03,150
their at the module level the local scope is
the same as the global scope so we can

138
00:13:03,150 --> 00:13:08,710
see that by printing both out at the end of
the program so lets add these couples of lines of output

139
00:13:08,710 --> 00:13:19,250
to just check that so...and if you run that

140
00:13:26,840 --> 00:13:30,870
and if we scroll across you should find that the basically
the two outputs from locals to globals

141
00:13:30,870 --> 00:13:34,240
is giving us the identical output here you can
see this looking pretty well

142
00:13:34,240 --> 00:13:39,300
identical right through to the end
everything is basically identical that's

143
00:13:39,300 --> 00:13:43,670
for those last two lines of output which
correspond to the output from line 22 to 23

144
00:13:45,290 --> 00:13:50,170
so that confirms that in the module scope locals and globals are the same

145
00:13:50,170 --> 00:13:55,040
second thing though is a bit odd and
you might be wondering about it if

146
00:13:55,040 --> 00:13:58,010
you paid careful attention to the
program's output since I added the non

147
00:13:58,010 --> 00:14:07,180
local variables so lets have a look at that again so spam 3 original
had a single variable called z but

148
00:14:07,180 --> 00:14:14,220
now you can see up here it also has
y as local and spam 2 you see that has

149
00:14:14,220 --> 00:14:19,610
now got an x its got the x as local as
well now this is very odd it seems to go

150
00:14:19,610 --> 00:14:23,730
against everything I have just discussed
about non local variables but in fact

151
00:14:23,730 --> 00:14:27,970
the explanation behavior of non local
variables is perfectly correct so

152
00:14:27,970 --> 00:14:32,400
there's nothing to worry about what we are
seeing is a result of the way the locals

153
00:14:32,400 --> 00:14:37,490
function works and some optimization of
the part of Python and that's because whether

154
00:14:37,490 --> 00:14:42,090
you use a non-local variable in the way
we've done python adds it to the local

155
00:14:42,090 --> 00:14:45,360
name space so that it doesn't have to
go hunting through all the enclosing

156
00:14:45,360 --> 00:14:49,760
scopes every time you use it so it
still referring to the non local variable

157
00:14:49,760 --> 00:14:55,810
its just a reference it in locals to make everything run faster and it might be worth your while

158
00:14:55,810 --> 00:15:03,770
having a look at the documentation for the locals function as well I'm just gonna bring that up on the screen

159
00:15:03,770 --> 00:15:06,770
theirs the link on the screen as you can see up there so we go to that

160
00:15:10,270 --> 00:15:18,470
theirs locals so a free variable is a variable that's
used in a scope where it has not been

161
00:15:18,470 --> 00:15:24,160
defined which is exactly the same thing
as a non-local variable when working

162
00:15:24,160 --> 00:15:27,420
with the scope of variables it is helpful
to remember the analogy

163
00:15:27,420 --> 00:15:33,670
LEGB and that stands for local enclosing
global built-ins and list the order to

164
00:15:33,670 --> 00:15:40,530
which Python searches for names so again
its local enclosing global and built-ins so

165
00:15:40,530 --> 00:15:43,910
closing refers to non-local or free
variables that I've discussed in this

166
00:15:43,910 --> 00:15:48,920
lecture now globals we have also seen and we have
looked at how to use it and assigned to

167
00:15:48,920 --> 00:15:54,820
global variables of course in our functions built in refers to the name that are built-in to Python

168
00:15:54,820 --> 00:15:58,360
and that we've been using throughout the course
we've got into the builtins.py module

169
00:15:58,360 --> 00:16:01,960
quite a few times now to have a look
at the various functions and built-ins are

170
00:16:01,960 --> 00:16:06,030
the final place that Python will possible look to when trying to resolve a

171
00:16:06,030 --> 00:16:10,800
name now if you remember and understand that LEGB in order and again that is local enclosing

172
00:16:10,800 --> 00:16:15,230
global built-ins if remember that
for resolving names and take note of the

173
00:16:15,230 --> 00:16:19,170
warnings that IntelliJ gives you should be
quite comfortable with the way Python

174
00:16:19,170 --> 00:16:23,740
scoping works and then should also
be able to use non-local and global variables

175
00:16:23,740 --> 00:16:24,650
when you need to

176
00:16:24,650 --> 00:16:31,210
ok finally that brings this section to an end it's been a very very long one we've

177
00:16:31,210 --> 00:16:34,430
looked at a few modules from the Python standard library and also the

178
00:16:34,430 --> 00:16:38,720
documentation so that other modules should
be much easier to use when a program

179
00:16:38,720 --> 00:16:42,770
needs the functionality they provide and
of course we also had a look at GUI

180
00:16:42,770 --> 00:16:47,880
programming called TKinter we did a lot with that but we are not done with that yet and theirs few more

181
00:16:47,880 --> 00:16:51,510
things we need to come before you can
use it to its full potential and then

182
00:16:51,510 --> 00:16:54,930
covered functions showing you how they are or what they are I should say and how to write your own

183
00:16:54,930 --> 00:16:59,990
and in the process we learn about scope in Python now classes are also

184
00:16:59,990 --> 00:17:01,440
created a new namespace

185
00:17:01,440 --> 00:17:05,160
and we are gonna be covering object oriented
programming in classes in the next section

186
00:17:05,160 --> 00:17:06,349
so I'll see you their

