WEBVTT 1 00:00:01.939 --> 00:00:07.020 so just as a recap from the previous video when we run this we do get an error 2 00:00:07.020 --> 00:00:12.869 and looking at the error we can see objects has no attribute power and the 3 00:00:12.869 --> 00:00:17.850 error is on line 45 which you can see on the screen 4 00:00:17.850 --> 00:00:23.080 and the reason for that is the Hamilton instance doesn't have a power 5 00:00:23.080 --> 00:00:27.650 attribute because we haven't created one by assigning a value to it like we did for 6 00:00:27.650 --> 00:00:33.170 the Kenwood instance on line 43 and this is the dynamic nature of Python that 7 00:00:33.170 --> 00:00:37.190 allows this kind of behaviour and you can easily end up with instances that are 8 00:00:37.190 --> 00:00:41.550 created from the same class template but which ultimately have 9 00:00:41.550 --> 00:00:46.800 different attributes and it can be useful feature but can also cause 10 00:00:46.800 --> 00:00:51.109 problems if you make a typing error when trying to assign a value to an existing 11 00:00:51.109 --> 00:00:56.190 data attribute for argument sake so it's worth paying careful attention to the 12 00:00:56.190 --> 00:01:00.760 IntelliJ tool tips that will appear after you type a do and to make sure that 13 00:01:00.760 --> 00:01:05.799 you're assigning it to attribute that you really expecting it to be so 14 00:01:05.799 --> 00:01:10.230 going back to the analogy of a class definition being like house plan that 15 00:01:10.230 --> 00:01:14.710 can be used to create houses in the real world theirs nothing to stop an extension 16 00:01:14.710 --> 00:01:18.090 being added to a house out after its built and that's pretty much what is happening 17 00:01:18.090 --> 00:01:23.210 here now of course subclassing which is where a new class is created from an 18 00:01:23.210 --> 00:01:28.400 existing one may be preferable to adding attributes to instances and will be 19 00:01:28.400 --> 00:01:32.750 looking at sub classes a little later there are ways to prevent this kind of 20 00:01:32.750 --> 00:01:37.590 behaviour and you can create classes in such a way that additional 21 00:01:37.590 --> 00:01:42.190 attributes can't be added to instances forcing classes to be sub class extra 22 00:01:42.190 --> 00:01:48.460 functionality is required but Python allow you to take either approach in now their is one aspect of 23 00:01:48.460 --> 00:01:52.320 classes that I want to mention before we start practicing all this by creating 24 00:01:52.320 --> 00:01:58.730 some more useful classes now I introduced to small talk term instance variable in 25 00:01:58.730 --> 00:02:03.260 the previous video and that's because it includes the words variable whereas data 26 00:02:03.260 --> 00:02:08.239 attribute does not and this is important because methods are also attributes of 27 00:02:08.239 --> 00:02:13.800 classes and you find the term attribute used to refer to both in the documentation 28 00:02:13.800 --> 00:02:18.240 so the terms data attribute and method are used to distinguish between the two 29 00:02:18.240 --> 00:02:23.650 types of attribute now the other aspect that I want to mention is that classes also 30 00:02:23.650 --> 00:02:27.880 have attributes and once again the Smalltalk term instance variable is 31 00:02:27.880 --> 00:02:33.050 useful because it contains the word instance now the data attributes in the 32 00:02:33.050 --> 00:02:38.790 kettle example as such as make and price have both been attributes of the instances and each 33 00:02:38.790 --> 00:02:43.630 instance has its own values for them so its also possible for the class to have 34 00:02:43.630 --> 00:02:47.790 attributes which is shared by all the instances so to stick with the analogy 35 00:02:47.790 --> 00:02:52.220 of a house plan each house that we built from the plan will have different attributes such 36 00:02:52.220 --> 00:02:56.890 as its address however all the houses will share some attributes such as the type of 37 00:02:56.890 --> 00:03:01.460 house now all house are built from a plan for a four-bedroom lodge would be four 38 00:03:01.460 --> 00:03:06.490 bedroom lodges so our kettle class is modelling electric kettles so we can 39 00:03:06.490 --> 00:03:10.020 introduce a class attribute called power source that all instances will share 40 00:03:11.220 --> 00:03:14.680 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:14.680 --> 00:03:28.260 class and on line 32 I'm going to add....so 42 00:03:28.260 --> 00:03:31.570 that's a class attribute that we've added called power source 43 00:03:32.930 --> 00:03:39.550 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:39.550 --> 00:03:47.830 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:47.830 --> 00:03:57.610 can try doing... 46 00:03:58.420 --> 00:04:00.530 ... 47 00:04:00.530 --> 00:04:10.459 .....now if you run that...you could see that all three have got 48 00:04:10.459 --> 00:04:16.000 electricity as the power source so the two instances kenwood and hamilton plus the class 49 00:04:16.000 --> 00:04:23.590 itself kettle have got this power source class attribute so think of that if you know 50 00:04:23.590 --> 00:04:29.520 Java or C++ this is very similar to static fields in those languages similar 51 00:04:29.520 --> 00:04:35.660 but not exactly the same but although its a useful comparison if you are used to 52 00:04:35.660 --> 00:04:40.030 those other languages don't take this to literally it is useful to examine the 53 00:04:40.030 --> 00:04:43.700 namespaces of the three objects to verify that the two instances are 54 00:04:43.700 --> 00:04:48.590 sharing the same attribute which only exists in the class and we can access the 55 00:04:48.590 --> 00:04:58.930 namespace via the dic attribute so lets go ahead and do that so... 56 00:04:58.930 --> 00:05:05.140 .... 57 00:05:05.140 --> 00:05:17.800 ....and 58 00:05:17.800 --> 00:05:26.570 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:26.570 --> 00:05:30.890 its a little bit better and looking at that output down the bottom of the screen the 60 00:05:30.890 --> 00:05:35.070 kettle class namespace contains quite a lot of items and we can see power 61 00:05:35.070 --> 00:05:39.020 source showing their and we can see some of the other functions we 62 00:05:39.020 --> 00:05:42.620 can see an init function and we should be able to see our switch on 63 00:05:42.620 --> 00:05:46.930 method if we scroll over and their is the switch on 64 00:05:46.930 --> 00:05:55.430 kettle.switch_on so that's their and the 2 instances below that you can see that 65 00:05:55.430 --> 00:06:02.100 they've got far less in their namespace and in fact theirs only the instance variables make price and on 66 00:06:02.100 --> 00:06:07.130 and you see Kenwood has got the extra power attribute that's the second from 67 00:06:07.130 --> 00:06:10.020 bottom one that we added earlier in this code but other than that 68 00:06:10.020 --> 00:06:14.180 their the same so what happens is that when we try to access the power_source 69 00:06:14.180 --> 00:06:19.409 attribute for the instances Python checks to see if the power source exists in the 70 00:06:19.409 --> 00:06:24.479 instance name space if it doesn't which is the case here it then checks the class 71 00:06:24.479 --> 00:06:28.680 for the instance and finds power source in the kettle class and that's why the 72 00:06:28.680 --> 00:06:32.780 reason it printed out because basically it got it from the class attribute so if 73 00:06:32.780 --> 00:06:40.080 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:40.080 --> 00:06:50.870 so.... 75 00:06:51.810 --> 00:06:54.810 .... 76 00:06:56.259 --> 00:07:04.069 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:04.069 --> 00:07:08.969 printouts are now all the same again as well and only updated the class attribute 78 00:07:08.969 --> 00:07:14.590 but it has changed the other two instances one automatically which is more or less sort of proof that the 79 00:07:14.590 --> 00:07:19.520 instances for Hamilton and kenwood are looking at the class attribute at that 80 00:07:19.520 --> 00:07:23.849 point and this is probably not surprising if Python is your first 81 00:07:23.849 --> 00:07:27.099 experience of object oriented programming and will probably make 82 00:07:27.099 --> 00:07:30.610 perfect sense after our discussion of global variables and functions that we've talk 83 00:07:30.610 --> 00:07:37.479 about previously so as long as we only access the class data attribute via the class rather than via 84 00:07:37.479 --> 00:07:44.139 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:44.139 --> 00:07:48.449 common power source in this scenario and show the same value in this case atomic 86 00:07:48.449 --> 00:07:52.340 but that's not true if you try and change this in different ways so if we 87 00:07:52.340 --> 00:08:02.639 come down to say the kenwood instance below their will do.... 88 00:08:02.639 --> 00:08:08.409 ... 89 00:08:09.919 --> 00:08:18.759 ...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:18.759 --> 00:08:21.430 as gas and Hamilton still showing atomic 91 00:08:21.430 --> 00:08:26.470 and the class attribute is still showing as atomic so this is probably not that 92 00:08:26.470 --> 00:08:31.170 surprising if Python is your first experience of object-oriented programming and would 93 00:08:31.170 --> 00:08:34.650 probably make perfect sense after our discussion in previous videos about 94 00:08:34.650 --> 00:08:38.830 global variables and functions remember that as soon as we try to assign a value 95 00:08:38.830 --> 00:08:40.660 to a global variable 96 00:08:40.660 --> 00:08:45.370 Python created a new local variable that shattered the global one so a 97 00:08:45.370 --> 00:08:49.860 similar thing is actually happening in this scenario when we added the code to 98 00:08:49.860 --> 00:08:57.670 switch the Kenwood power source to gas the code on line 53 so although kenwood 99 00:08:57.670 --> 00:09:01.580 is now running on gas hamilton's is still running on atomic power which is one of 100 00:09:01.580 --> 00:09:06.640 the ways that class attributes differ from Java static fields and examining 101 00:09:06.640 --> 00:09:14.670 the source if we run this again and look at the namespaces notice now that Kenwood second to 102 00:09:14.670 --> 00:09:18.890 bottom one has now got its own data attribute called power_source that 103 00:09:18.890 --> 00:09:22.920 shadows the class attribute so this is definitely something to watch out for as 104 00:09:22.920 --> 00:09:27.480 it's quite easy to make the mistake of assigning a new value to a class attribute 105 00:09:27.480 --> 00:09:31.390 via an instance variable and Java will give you a warning if you try to access 106 00:09:31.390 --> 00:09:35.620 as a static field via an instance variable but you won't get any such warnings in 107 00:09:35.620 --> 00:09:41.940 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:41.940 --> 00:09:45.800 there and it may not make complete sense at the moment but don't worry because I'm 109 00:09:45.800 --> 00:09:49.010 gonna be using a lot of examples in the next set of videos that will consolidate 110 00:09:49.010 --> 00:09:53.480 what's been covered so far a simple kettle class doesn't really do very much 111 00:09:53.480 --> 00:09:58.880 but as we create more class and using them to encapsulate data and functionality it'll 112 00:09:58.880 --> 00:10:02.610 start making a lot more sense now also we've introduced other object-oriented 113 00:10:02.610 --> 00:10:06.490 concepts such as inheritance and composition so that we can build more 114 00:10:06.490 --> 00:10:11.280 complicated class structures and the other thing I do is also introduce other 115 00:10:11.280 --> 00:10:16.180 object-oriented concepts such as inheritance and composition so that we 116 00:10:16.180 --> 00:10:19.970 can build more complicated class structures so we'll start off slow in 117 00:10:19.970 --> 00:10:23.410 the next few videos will look at some more examples of using classes see you 118 00:10:23.410 --> 00:10:23.830 in the next video