1
00:00:05,540 --> 00:00:10,630
so just as a recap from the previous
video when we run this we do get an error

2
00:00:10,630 --> 00:00:16,470
and looking at the error we can see objects has no attribute power and the

3
00:00:16,470 --> 00:00:21,460
error is on line 45 which you can
see on the screen

4
00:00:21,460 --> 00:00:26,690
and the reason for that is the Hamilton
instance doesn't have a power

5
00:00:26,690 --> 00:00:31,260
attribute because we haven't created one by
assigning a value to it like we did for

6
00:00:31,260 --> 00:00:36,780
the Kenwood instance on line 43 and this is
the dynamic nature of Python that

7
00:00:36,780 --> 00:00:40,800
allows this kind of behaviour and you can
easily end up with instances that are

8
00:00:40,800 --> 00:00:45,160
created from the same class
template but which ultimately have

9
00:00:45,160 --> 00:00:50,410
different attributes and it can be
useful feature but can also cause

10
00:00:50,410 --> 00:00:54,710
problems if you make a typing error when
trying to assign a value to an existing

11
00:00:54,710 --> 00:00:59,800
data attribute for argument sake so it's
worth paying careful attention to the

12
00:00:59,800 --> 00:01:04,370
IntelliJ tool tips that will appear after
you type a do and to make sure that

13
00:01:04,370 --> 00:01:09,400
you're assigning it to attribute that you really expecting it to be so

14
00:01:09,400 --> 00:01:13,840
going back to the analogy of a class
definition being like house plan that

15
00:01:13,840 --> 00:01:18,320
can be used to create houses in the real
world theirs nothing to stop an extension

16
00:01:18,320 --> 00:01:21,700
being added to a house out after its built and
that's pretty much what is happening

17
00:01:21,700 --> 00:01:26,820
here now of course subclassing which is
where a new class is created from an

18
00:01:26,820 --> 00:01:32,010
existing one may be preferable to adding
attributes to instances and will be

19
00:01:32,010 --> 00:01:36,360
looking at sub classes a little later
there are ways to prevent this kind of

20
00:01:36,360 --> 00:01:41,200
behaviour and you can create classes in
such a way that additional

21
00:01:41,200 --> 00:01:45,800
attributes can't be added to instances
forcing classes to be sub class extra

22
00:01:45,800 --> 00:01:52,070
functionality is required but Python allow you to take either approach in now their is one aspect of

23
00:01:52,070 --> 00:01:55,930
classes that I want to mention before we
start practicing all this by creating

24
00:01:55,930 --> 00:02:02,340
some more useful classes now I introduced
to small talk term instance variable in

25
00:02:02,340 --> 00:02:06,870
the previous video and that's because it
includes the words variable whereas data

26
00:02:06,870 --> 00:02:11,840
attribute does not and this is important
because methods are also attributes of

27
00:02:11,840 --> 00:02:17,410
classes and you find the term attribute used to
refer to both in the documentation

28
00:02:17,410 --> 00:02:21,850
so the terms data attribute and method are
used to distinguish between the two

29
00:02:21,850 --> 00:02:27,260
types of attribute now the other aspect that I want to mention is that classes also

30
00:02:27,260 --> 00:02:31,490
have attributes and once again the
Smalltalk term instance variable is

31
00:02:31,490 --> 00:02:36,660
useful because it contains the word
instance now the data attributes in the

32
00:02:36,660 --> 00:02:42,400
kettle example as such as make and price have both been attributes of the instances and each

33
00:02:42,400 --> 00:02:47,240
instance has its own values for them so its
also possible for the class to have

34
00:02:47,240 --> 00:02:51,400
attributes which is shared by all the
instances so to stick with the analogy

35
00:02:51,400 --> 00:02:55,830
of a house plan each house that we built from the
plan will have different attributes such

36
00:02:55,830 --> 00:03:00,500
as its address however all the houses will share some attributes such as the type of

37
00:03:00,500 --> 00:03:05,070
house now all house are built from a plan for a four-bedroom lodge would be four

38
00:03:05,070 --> 00:03:10,100
bedroom lodges so our kettle class is modelling electric kettles so we can

39
00:03:10,100 --> 00:03:13,630
introduce a class attribute called power source
that all instances will share

40
00:03:14,830 --> 00:03:18,290
so lets go ahead and do that so we are gonna go back up to
the top of the definition for the kettle

41
00:03:18,290 --> 00:03:31,870
class and on line 32 I'm going to add....so

42
00:03:31,870 --> 00:03:35,180
that's a class attribute that we've added called power source

43
00:03:36,540 --> 00:03:43,160
so now we've done that we can demonstrate that all instances share a single version of this class attribute so we are gonna add the following

44
00:03:43,160 --> 00:03:51,440
at the end of the file but first I'm going to comment out line 47 to remove that error so we

45
00:03:51,440 --> 00:04:01,220
can try doing...

46
00:04:02,030 --> 00:04:04,140
...

47
00:04:04,140 --> 00:04:14,060
.....now if you run that...you could see that all three have got

48
00:04:14,060 --> 00:04:19,610
electricity as the power source so the two
instances kenwood and hamilton plus the class

49
00:04:19,610 --> 00:04:27,200
itself kettle have got this power source class
attribute so think of that if you know

50
00:04:27,200 --> 00:04:33,130
Java or C++ this is very similar to
static fields in those languages similar

51
00:04:33,130 --> 00:04:39,270
but not exactly the same but although its a
useful comparison if you are used to

52
00:04:39,270 --> 00:04:43,640
those other languages don't take this to
literally it is useful to examine the

53
00:04:43,640 --> 00:04:47,310
namespaces of the three objects to
verify that the two instances are

54
00:04:47,310 --> 00:04:52,200
sharing the same attribute which only
exists in the class and we can access the

55
00:04:52,200 --> 00:05:02,540
namespace via the dic attribute so lets go ahead and do that so...

56
00:05:02,540 --> 00:05:08,750
....

57
00:05:08,750 --> 00:05:21,410
....and

58
00:05:21,410 --> 00:05:30,180
I'm going to change or run it first move this as a
bit more space I'm gonna move to bottom we can see

59
00:05:30,180 --> 00:05:34,500
its a little bit better and looking at that
output down the bottom of the screen the

60
00:05:34,500 --> 00:05:38,680
kettle class namespace contains quite a
lot of items and we can see power

61
00:05:38,680 --> 00:05:42,630
source showing their and we can see some of the other functions we

62
00:05:42,630 --> 00:05:46,230
can see an init function and we
should be able to see our switch on

63
00:05:46,230 --> 00:05:50,540
method if we scroll over and their is the switch on

64
00:05:50,540 --> 00:05:59,040
kettle.switch_on so that's their and the 2
instances below that you can see that

65
00:05:59,040 --> 00:06:05,710
they've got far less in their namespace and in fact theirs only
the instance variables make price and on

66
00:06:05,710 --> 00:06:10,740
and you see Kenwood has got the extra power
attribute that's the second from

67
00:06:10,740 --> 00:06:13,630
bottom one that we added earlier in this
code but other than that

68
00:06:13,630 --> 00:06:17,790
their the same so what happens is that
when we try to access the power_source

69
00:06:17,790 --> 00:06:23,010
attribute for the instances Python checks
to see if the power source exists in the

70
00:06:23,010 --> 00:06:28,080
instance name space if it doesn't which
is the case here it then checks the class

71
00:06:28,080 --> 00:06:32,290
for the instance and finds power source
in the kettle class and that's why the

72
00:06:32,290 --> 00:06:36,390
reason it printed out because basically
it got it from the class attribute so if

73
00:06:36,390 --> 00:06:43,690
you close it down if you go ahead and make a
change with the power source if we do something like this

74
00:06:43,690 --> 00:06:54,480
so....

75
00:06:55,420 --> 00:06:58,420
....

76
00:06:59,860 --> 00:07:07,670
so if we run this again you can see what happens there is that atomic we are switching to atomic power and the three

77
00:07:07,670 --> 00:07:12,570
printouts are now all the same again as well and only updated the class attribute

78
00:07:12,570 --> 00:07:18,200
but it has changed the other two instances one automatically which is more or less sort of proof that the

79
00:07:18,200 --> 00:07:23,130
instances for Hamilton and kenwood are
looking at the class attribute at that

80
00:07:23,130 --> 00:07:27,450
point and this is probably not
surprising if Python is your first

81
00:07:27,450 --> 00:07:30,700
experience of object oriented
programming and will probably make

82
00:07:30,700 --> 00:07:34,220
perfect sense after our discussion of global
variables and functions that we've talk

83
00:07:34,220 --> 00:07:41,080
about previously so as long as we only access the class
data attribute via the class rather than via

84
00:07:41,080 --> 00:07:47,740
instance then when we want to assign a new value to it this works as expected so all 3 objects are still sharing their

85
00:07:47,740 --> 00:07:52,050
common power source in this scenario and
show the same value in this case atomic

86
00:07:52,050 --> 00:07:55,950
but that's not true if you try and
change this in different ways so if we

87
00:07:55,950 --> 00:08:06,240
come down to say the kenwood instance below their will do....

88
00:08:06,240 --> 00:08:12,010
...

89
00:08:13,520 --> 00:08:22,360
...so if we run that now...you can see in the bottom here we've got atomic for the class attribute we change kenwood to gas and that is showing

90
00:08:22,360 --> 00:08:25,040
as gas and Hamilton still showing
atomic

91
00:08:25,040 --> 00:08:30,080
and the class attribute is still showing
as atomic so this is probably not that

92
00:08:30,080 --> 00:08:34,780
surprising if Python is your first experience
of object-oriented programming and would

93
00:08:34,780 --> 00:08:38,260
probably make perfect sense after our
discussion in previous videos about

94
00:08:38,260 --> 00:08:42,440
global variables and functions remember
that as soon as we try to assign a value

95
00:08:42,440 --> 00:08:44,270
to a global variable

96
00:08:44,270 --> 00:08:48,980
Python created a new local variable that
shattered the global one so a

97
00:08:48,980 --> 00:08:53,470
similar thing is actually happening in
this scenario when we added the code to

98
00:08:53,470 --> 00:09:01,280
switch the Kenwood power source to gas the code on line 53 so although kenwood

99
00:09:01,280 --> 00:09:05,190
is now running on gas hamilton's is still
running on atomic power which is one of

100
00:09:05,190 --> 00:09:10,250
the ways that class attributes differ
from Java static fields and examining

101
00:09:10,250 --> 00:09:18,280
the source if we run this again and
look at the namespaces notice now that Kenwood second to

102
00:09:18,280 --> 00:09:22,500
bottom one has now got its own data attribute
called power_source that

103
00:09:22,500 --> 00:09:26,530
shadows the class attribute so this is
definitely something to watch out for as

104
00:09:26,530 --> 00:09:31,090
it's quite easy to make the mistake of
assigning a new value to a class attribute

105
00:09:31,090 --> 00:09:35,000
via an instance variable and Java will give you a warning if you try to access

106
00:09:35,000 --> 00:09:39,230
as a static field via an instance variable
but you won't get any such warnings in

107
00:09:39,230 --> 00:09:45,550
Python so that's the basic of classes
and their is a quite a bit of jargon and new concepts that we've thrown in

108
00:09:45,550 --> 00:09:49,410
there and it may not make complete sense
at the moment but don't worry because I'm

109
00:09:49,410 --> 00:09:52,620
gonna be using a lot of examples in the
next set of videos that will consolidate

110
00:09:52,620 --> 00:09:57,090
what's been covered so far a simple kettle class doesn't really do very much

111
00:09:57,090 --> 00:10:02,490
but as we create more class and using them to
encapsulate data and functionality it'll

112
00:10:02,490 --> 00:10:06,220
start making a lot more sense now
also we've introduced other object-oriented

113
00:10:06,220 --> 00:10:10,100
concepts such as inheritance and
composition so that we can build more

114
00:10:10,100 --> 00:10:14,890
complicated class structures and the
other thing I do is also introduce other

115
00:10:14,890 --> 00:10:19,790
object-oriented concepts such as
inheritance and composition so that we

116
00:10:19,790 --> 00:10:23,580
can build more complicated class
structures so we'll start off slow in

117
00:10:23,580 --> 00:10:27,020
the next few videos will look at some
more examples of using classes see you

118
00:10:27,020 --> 00:10:27,440
in the next video

