WEBVTT 1 00:00:01.960 --> 00:00:07.259 so continuing on from our previous video so we ended up talking about variables 2 00:00:07.259 --> 00:00:11.620 but saying that when a variable is bound to an instance of a class in 3 00:00:11.620 --> 00:00:16.430 Python its referred to as a data attribute now their other object orientated 4 00:00:16.430 --> 00:00:21.079 terms for the same thing so you might find variable attributes also called 5 00:00:21.079 --> 00:00:27.119 fields in languages such as Java or data members in C++ and I'll introduced yet 6 00:00:27.119 --> 00:00:28.360 another term shortly 7 00:00:28.360 --> 00:00:32.250 one borrowed from the small talk language but I over simplified things 8 00:00:32.250 --> 00:00:37.140 here and now we will be talking about different kinds of methods and 9 00:00:37.140 --> 00:00:42.570 attributes later but that's the basic jargon relating to classes which you 10 00:00:42.570 --> 00:00:46.589 should probably become familiar with now before I summarise these terms 11 00:00:46.589 --> 00:00:50.320 there's another way to use a string replacement fields that is useful when 12 00:00:50.320 --> 00:00:55.999 dealing with classes so because Kenwood and Hamilton are objects we can specify 13 00:00:55.999 --> 00:01:00.269 their attributes in the replacement fields and let's have a look at that 14 00:01:00.269 --> 00:01:04.850 to see how we can do that so we are gonna add that on line 20 we type.... 15 00:01:11.010 --> 00:01:25.160 .... 16 00:01:26.970 --> 00:01:40.370 ...so here because Kenwood and 17 00:01:40.370 --> 00:01:44.260 Hamilton are objects we can specify their attributes in the replacement 18 00:01:44.260 --> 00:01:48.590 fields so if we run this just to confirm we should see the identical results the 19 00:01:48.590 --> 00:01:57.780 print out as you can see their exactly the same but looking at the code I will argue that line 20 is easy to read because 20 00:01:57.780 --> 00:02:02.090 you can see immediately which attributes are being used at each point in the 21 00:02:02.090 --> 00:02:06.500 string and of course on line 18 you couldn't see that because there wasn't anything in the 22 00:02:06.500 --> 00:02:13.980 braces so just to summarize where we are at just gonna paste in that code in their now paste in those 23 00:02:13.980 --> 00:02:19.830 comments to be more precise so a class is a template for creating objects all 24 00:02:19.830 --> 00:02:23.250 objects created using the same class will have the same characteristics 25 00:02:23.250 --> 00:02:30.660 object is an instance of a class instantiate we didn't used the word instantiate but 26 00:02:30.660 --> 00:02:36.010 that is how the process of creating an instance of a class method is a function 27 00:02:36.010 --> 00:02:41.269 defined in a class and attribute is a variable bound to an instance of a 28 00:02:41.269 --> 00:02:46.690 class so as you can see here a class is a template for creating objects the 29 00:02:46.690 --> 00:02:50.110 template defines the attributes and methods that all instances of the class 30 00:02:50.110 --> 00:02:52.880 will have now although this is not true 31 00:02:52.880 --> 00:02:57.250 of other object oriented programming languages in Python a class is also 32 00:02:57.250 --> 00:03:02.370 exactly the same thing as a type other languages might refer to Kingwood in 33 00:03:02.370 --> 00:03:07.459 our sample program is being of type kettle but they also have 34 00:03:07.459 --> 00:03:13.550 types that are not classes but in Python every type is a class which is why we 35 00:03:13.550 --> 00:03:17.610 were able to call the add method of our int object at the start of the previous 36 00:03:17.610 --> 00:03:18.530 video 37 00:03:18.530 --> 00:03:22.830 an object is another name for an instance of a class then once the class 38 00:03:22.830 --> 00:03:27.280 has been defined it is the actual instances that are used in a program to 39 00:03:27.280 --> 00:03:30.940 store the values of the attributes and perform actions on them and you saw 40 00:03:30.940 --> 00:03:35.580 instances being created on line 9 and line 16 on the screen so when 41 00:03:35.580 --> 00:03:39.709 creating a new object from a class definition the terms uses instantiating 42 00:03:39.709 --> 00:03:44.510 an object and that's just a fancy word for creating an instance of a class but 43 00:03:44.510 --> 00:03:49.350 you should be familiar with that term now method is a function as bound to an 44 00:03:49.350 --> 00:03:53.310 instance of a class now in the Tkinter videos you might recall we created 45 00:03:53.310 --> 00:03:57.730 instances of the back label and canvas classes and we used their grid and pack 46 00:03:57.730 --> 00:04:03.300 methods to place them on the screen and finally an attribute is a variable that 47 00:04:03.300 --> 00:04:07.870 is bound to an instance of a class so the kettle variable and kenwood can keep 48 00:04:07.870 --> 00:04:12.290 track of the values of its making price without affecting the attributes 49 00:04:12.290 --> 00:04:18.489 of the Hamilton object which has got its own values for those attributes so as I 50 00:04:18.489 --> 00:04:22.360 hinted at earlier this is a slight over-simplification in the last two 51 00:04:22.360 --> 00:04:26.260 definitions and i will be explaining a bit more about that a little bit later on in the 52 00:04:26.260 --> 00:04:30.229 section but we have to walk here before we can run so those definitions will give 53 00:04:30.229 --> 00:04:34.210 us something to work with while we get the hang of classes and object oriented 54 00:04:34.210 --> 00:04:39.240 programming in general so continuing on now with our kettle class at the moment 55 00:04:39.240 --> 00:04:44.180 it has 2 data attributes make and price and a single method called init 56 00:04:44.180 --> 00:04:47.360 __init__ which you can see is 57 00:04:47.360 --> 00:04:52.090 showing on line three and if you look at that lit looks like it's sets the values for 58 00:04:52.090 --> 00:04:55.090 the attributes but also looks a little strange 59 00:04:55.660 --> 00:05:00.419 now as I explain what's going on here I'm gonna be referring to quite a few things whos names 60 00:05:00.419 --> 00:05:05.220 start with and end with 2 underscores and it's going to get a lil bit tedious 61 00:05:05.220 --> 00:05:08.710 if I keep saying __ all the time so I'm just going to use the name 62 00:05:08.710 --> 00:05:12.370 of the object and you can see on the screen that its got underscores around 63 00:05:12.370 --> 00:05:16.050 it but I will review this use of underscores and how it relates to 64 00:05:16.050 --> 00:05:19.720 classes later on but just keep in mind that time when we refer to something for 65 00:05:19.720 --> 00:05:23.110 example on line 3 as init I'm referring to __init__ 66 00:05:23.110 --> 00:05:28.310 now we looked at most of the users of underscore around 67 00:05:28.310 --> 00:05:30.300 variable names in the previous section 68 00:05:30.300 --> 00:05:35.210 and theirs a little bit more to add to that once we get more into classes now this 69 00:05:35.210 --> 00:05:35.940 class 70 00:05:35.940 --> 00:05:40.610 obviously doesn't do a great deal and theirs not really a lot going on and it's 71 00:05:40.610 --> 00:05:44.000 not really a lot you can do with a kettle other than switching it on and wait but 72 00:05:44.000 --> 00:05:49.210 I will add a method to do that before looking at more complicated classes now the Python 73 00:05:49.210 --> 00:05:51.509 style guide pep8 by the way 74 00:05:51.509 --> 00:05:55.590 recommends just single blank line between methods inside a class which is 75 00:05:55.590 --> 00:06:00.110 different to the recommendation and there should be two blank lines between 76 00:06:00.110 --> 00:06:05.330 top level functions in a module so if you use 2 blanks lines then IntelliJ will show 77 00:06:05.330 --> 00:06:09.050 warning and personally I like to leave 2 blank lines after class definitions 78 00:06:09.050 --> 00:06:13.599 but this is not covered in the style guide and you won't get a warning unless you leave more than two blank 79 00:06:13.599 --> 00:06:14.930 lines and 80 00:06:14.930 --> 00:06:17.930 I'm gonna add a second method now 81 00:06:19.580 --> 00:06:30.870 and if we start this....and notice as I press the left bracket 82 00:06:30.870 --> 00:06:36.099 IntelliJ automatically added the self parameter so as I mentioned functions that are bound to a 83 00:06:36.099 --> 00:06:40.830 class called methods and main difference between a method and a function is the 84 00:06:40.830 --> 00:06:45.690 presence of this self parameter that's been added automatically just go ahead and 85 00:06:45.690 --> 00:06:57.960 finish this method....and just a reminder if I go to and add that as a 86 00:06:57.960 --> 00:07:02.879 second add 2 lines between methods we get the warning from IntelliJ 2 87 00:07:02.879 --> 00:07:04.080 blanks lines so if your in a 88 00:07:04.080 --> 00:07:10.639 class only have one line between methods to remove that warning and you might be 89 00:07:10.639 --> 00:07:14.500 wondering what is the self parameter well so there's nothing really special 90 00:07:14.500 --> 00:07:19.500 about the name self or the parameter and you have called it anything you wanted avoiding 91 00:07:19.500 --> 00:07:24.449 name conflicts with other objects of course but unlike to key word this in Java for 92 00:07:24.449 --> 00:07:29.210 example self is just the name of a parameter the important thing is the 93 00:07:29.210 --> 00:07:33.210 presence of the parameter not what its called so having said that the 94 00:07:33.210 --> 00:07:37.259 convention is that it's called self and although this is not enforced in any way 95 00:07:37.259 --> 00:07:41.310 it's not a good idea to change the convention is so well established that 96 00:07:41.310 --> 00:07:43.460 tools may expect to be called self and may not 97 00:07:43.460 --> 00:07:48.720 work if you use another name in fact as we have just seen IntelliJ automatically use the 98 00:07:48.720 --> 00:07:50.139 name self for it 99 00:07:50.139 --> 00:07:54.120 when we are defining the method just now so what is self 100 00:07:54.120 --> 00:07:59.080 well the answer is that it's a reference to the instance of the class now if you 101 00:07:59.080 --> 00:08:03.970 used to C++ or Java then you can think of it as equivalent to this and it's 102 00:08:03.970 --> 00:08:08.039 used to refer to instance variables and you could see that we're doing that 103 00:08:08.039 --> 00:08:14.590 on lines four five and six and again on line 9 and although we've use many methods in the early 104 00:08:14.590 --> 00:08:18.210 lectures in this course we've never had to specify an argument for the self 105 00:08:18.210 --> 00:08:22.460 parameter and this is because Python supplies the value automatically when 106 00:08:22.460 --> 00:08:26.120 calling the method now I will talked about this a little bit more in a minute but first 107 00:08:26.120 --> 00:08:38.270 let's call the method and check to see if it is a kettle or not so we type.... 108 00:08:38.270 --> 00:08:50.130 ....and notice that again when I typed that.... 109 00:08:50.130 --> 00:08:55.640 ....you can see the definition shows the self parameter but when I press enter it automatically 110 00:08:55.640 --> 00:08:59.540 there's no parameter passed in the brackets and if we just confirm that.... 111 00:08:59.540 --> 00:09:11.329 ...and if we run this first time we ran it on line 33 was false 112 00:09:11.329 --> 00:09:16.230 that's because up here on line six we defined it as false second time after 113 00:09:16.230 --> 00:09:21.779 calling Hamilton.switch_on the variable the attribute in this case for 114 00:09:21.779 --> 00:09:27.770 our hamilton instance was set to true in other words the value of the hamiltons instance on 115 00:09:27.770 --> 00:09:31.490 started off as false because that's what it was initialized to in the constructor 116 00:09:31.490 --> 00:09:36.740 and calling the switch-on method on line 34 set to true which is the value of 117 00:09:36.740 --> 00:09:42.260 course we are getting when we print out the value for the second time on line 35 now the 118 00:09:42.260 --> 00:09:47.079 term constructor by the way refers to a special method that is executed when an 119 00:09:47.079 --> 00:09:51.890 instance of a class is created or constructed in Python this is the init 120 00:09:51.890 --> 00:09:54.480 method that you can see defined on line 3 121 00:09:54.480 --> 00:09:59.459 and as you saw when I typed in line 34 we don't provide a value for the 122 00:09:59.459 --> 00:10:03.300 self parameter when calling the switch_on method when you call a method 123 00:10:03.300 --> 00:10:04.600 on an instance 124 00:10:04.600 --> 00:10:08.860 Python automatically provides a reference to the instance as the first parameter to 125 00:10:08.860 --> 00:10:13.410 the method now we can see this in a bit more detail by not using and instance but 126 00:10:13.410 --> 00:10:18.160 calling the method the class itself instead so we come down here and put this 127 00:10:18.160 --> 00:10:27.089 on lines 37 we can type.... 128 00:10:27.089 --> 00:10:39.339 ...so if we run that you can see that we got the value true 129 00:10:39.339 --> 00:10:43.250 and of course Hamilton initially when we created the instance of the 130 00:10:43.250 --> 00:10:48.769 object was then constructor made it the value false that by calling this method in an 131 00:10:48.769 --> 00:10:53.389 alternative ways you can see they're using the class definition initially you 132 00:10:53.389 --> 00:10:57.260 can see what's happening there and the parameters has been passed so instead of 133 00:10:57.260 --> 00:11:01.720 using an instance of kettle to call a method we've use the class itself because 134 00:11:01.720 --> 00:11:06.139 there is no instance here the value for self has to be specified and failing to do so 135 00:11:06.139 --> 00:11:11.589 would give an error so the two forms have exactly the same effect and as you can see 136 00:11:11.589 --> 00:11:16.269 when we ran the program kettle switch on ended up returning true so we can see 137 00:11:16.269 --> 00:11:20.800 that it was updated correctly so lets go back to the definition again I just want to 138 00:11:20.800 --> 00:11:28.630 go back and delete this out...just to confirm that we may have miss this the first and when I add the bracket not the parameter 139 00:11:28.630 --> 00:11:32.410 showing up there as self so its asking for that parameter in this case and I can 140 00:11:32.410 --> 00:11:37.260 change it too kenwood but again just to confirm if we do the 141 00:11:37.260 --> 00:11:46.160 alternative syntax......so when I do that 142 00:11:46.160 --> 00:11:50.440 noting that the self parameter is showing at the top of the pop up but when 143 00:11:50.440 --> 00:11:55.560 I press enter notice how it didn't ask for the parameter in this case because it knows 144 00:11:55.560 --> 00:11:58.510 we've already got an instance that we're working on 145 00:11:58.510 --> 00:12:02.460 so the implementation of classes in Python can create and some unexpected 146 00:12:02.460 --> 00:12:03.850 behavior 147 00:12:03.850 --> 00:12:08.080 partly because it's a mix of modular three and C++ with some small talk thrown 148 00:12:08.080 --> 00:12:12.200 in for good measure and once again if you are learning object oriented programming for 149 00:12:12.200 --> 00:12:16.080 the first time using Python then you'll probably be quite happy with the way 150 00:12:16.080 --> 00:12:20.440 things work that's not to say you won't have to concentrate but there shouldn't be 151 00:12:20.440 --> 00:12:27.370 any surprises but for C++ and Java programmers it might be a good idea to put what you already know to the back 152 00:12:27.370 --> 00:12:31.610 of your mind and be prepared to learn a new way of thinking about things when it 153 00:12:31.610 --> 00:12:34.710 comes to classes and object-oriented programming in Python 154 00:12:35.710 --> 00:12:39.740 now just like everything else in Python classes are dynamic and it can be modified 155 00:12:39.740 --> 00:12:43.930 after their created now to see this in action and reinforce my previous comment 156 00:12:43.930 --> 00:12:47.990 about surprises for C++ and Java programmers I'm going to add a measure 157 00:12:47.990 --> 00:12:52.730 for the keyword kettles power and kilowatts let's go ahead and do that so 158 00:12:52.730 --> 00:13:00.030 come down here to line 41 a bit more space so we can see..... 159 00:13:00.030 --> 00:13:18.370 .... 160 00:13:24.270 --> 00:13:28.750 so this case now the Kenwood instance of the kettle class now has another 161 00:13:28.750 --> 00:13:34.770 data attribute called power with the value 1.5 now remember that variables 162 00:13:34.770 --> 00:13:38.610 in Python come into existence the first time they're assigned a value and the 163 00:13:38.610 --> 00:13:43.050 same thing is true for instance variables which is the small talk name for data 164 00:13:43.050 --> 00:13:48.440 attributes so here I created a data attribute for the Kenwood object called power 165 00:13:48.440 --> 00:13:55.240 and I gave it a value of 1.5 so the term instance variable is useful because it describes 166 00:13:55.240 --> 00:14:00.810 power as a variable that is bound to an instance of the kettle class so as a 167 00:14:00.810 --> 00:14:07.050 result the following code I'm gonna add shouldn't be surprising..... 168 00:14:07.050 --> 00:14:22.590 ...and if we run this we get an error message attribute kettle object has not attribute power so if I close that 169 00:14:22.590 --> 00:14:27.500 down again and that's because the Hamilton instance doesn't have a power attribute 170 00:14:27.500 --> 00:14:32.470 because if you're not created one by assigning a value to it on line 43 we added 171 00:14:32.470 --> 00:14:37.240 an instance variable for the Kenwood instance of the kettle class but of course we didn't 172 00:14:37.240 --> 00:14:41.010 do that for the Hamilton instance and that's why we got an error when we run 173 00:14:41.010 --> 00:14:41.810 the program 174 00:14:41.810 --> 00:14:46.580 alright so I'm going to finish this video here now in the next video we're going to talk more 175 00:14:46.580 --> 00:14:50.160 about objects and find a bit more about instance variables 176 00:14:50.660 --> 00:14:56.310 and continue on generally speaking in our discussion and in our journey finding out more 177 00:14:56.310 --> 00:14:59.350 about object oriented programming in Python see you in the next video