WEBVTT 1 00:00:01.939 --> 00:00:05.950 so there's several different programming paradigms and up until now in this 2 00:00:05.950 --> 00:00:09.440 course we've been mainly using the imperative style of programming 3 00:00:09.440 --> 00:00:14.119 imperative programming involves providing a series of instructions for 4 00:00:14.119 --> 00:00:18.510 the computer to follow in a defined order now object oriented programming 5 00:00:18.510 --> 00:00:24.430 aims to combine data and the processes that act on that data into objects which is 6 00:00:24.430 --> 00:00:26.460 called encapsulation 7 00:00:26.460 --> 00:00:30.390 before I continue it's important to realize that the different paradigms are 8 00:00:30.390 --> 00:00:34.550 not necessarily exclusive and object-oriented programming makes use of 9 00:00:34.550 --> 00:00:38.429 imperative programming within the methods that objects used to manipulate 10 00:00:38.429 --> 00:00:44.440 the data also as we will see shortly our imperative programming in Python has 11 00:00:44.440 --> 00:00:49.530 actually made extensive use of objects we just didn't know that so the approach that 12 00:00:49.530 --> 00:00:54.569 we've been using so far in the course is very similar to a recipe for cooking a 13 00:00:54.569 --> 00:00:58.710 meal so the recipe starts with the ingredients and utensils it will be need 14 00:00:58.710 --> 00:01:03.479 which corresponds to a programs data and then continues with the description of 15 00:01:03.479 --> 00:01:07.890 the steps that must be performed on that data to produce in this case for the 16 00:01:07.890 --> 00:01:08.630 recipe 17 00:01:08.630 --> 00:01:13.490 finished meal an object oriented approach relies on the objects such as 18 00:01:13.490 --> 00:01:18.630 the eggs milk spoon etc knowing how to perform certain operations so 19 00:01:18.630 --> 00:01:21.010 the program just tells them to do so 20 00:01:21.010 --> 00:01:26.000 concept of an egg that knows how to fry itself is a little bizarre but that 21 00:01:26.000 --> 00:01:29.560 really shows that the imperatives approach to cooking a meal is more 22 00:01:29.560 --> 00:01:32.560 appropriate than an object-oriented approach 23 00:01:33.280 --> 00:01:37.270 now a good example of the encapsulation of data and function might be self lighting 24 00:01:37.270 --> 00:01:40.680 cigarettes well actually its a bad example and I certainly wouldn't 25 00:01:40.680 --> 00:01:45.210 want to suggest that smoking is good but it does demonstrate the concept quite well 26 00:01:45.210 --> 00:01:50.090 so without object oriented programming the steps to smoking a cigarette might 27 00:01:50.090 --> 00:01:54.910 be place cigarette in mouth that's number one number two light match number three 28 00:01:54.910 --> 00:02:00.020 hold match to cigarette and number four inhale now encapsulating the functions 29 00:02:00.020 --> 00:02:04.830 that can be performed on the cigarette into the cigarette reduces the code to 30 00:02:04.830 --> 00:02:10.600 place cigarette in mouth and inhale so the cigarette knows how to light itself so 31 00:02:10.600 --> 00:02:13.560 that's not something that the smoker needs to be concerned with 32 00:02:13.560 --> 00:02:18.330 the details of how it lights itself are built into it when it's manufactured or 33 00:02:18.330 --> 00:02:21.980 in the case of programming it's programmed into it when it's defined 34 00:02:21.980 --> 00:02:26.620 and the actual implementation is of no interest to anything that uses the 35 00:02:26.620 --> 00:02:30.849 object now I've exercise a little license they're self lighting cigarettes were 36 00:02:30.849 --> 00:02:35.360 produced for short time the 1960's coincidentally around the same time that 37 00:02:35.360 --> 00:02:39.650 object oriented programming was conceived and had a igniter 38 00:02:39.650 --> 00:02:44.680 built into them that had to be struck like a match the advanced type that I described only 39 00:02:44.680 --> 00:02:49.750 existed in science fiction stories now modern electronic cigarettes take this 40 00:02:49.750 --> 00:02:53.160 concept a little bit further but it's only the details of the implementation 41 00:02:53.160 --> 00:02:57.930 that have changed so the method use remains pretty much the same as the 42 00:02:57.930 --> 00:03:03.060 science-fiction variety now that's important concept of object oriented 43 00:03:03.060 --> 00:03:07.400 programming as we will see replacing one object with another one that performs 44 00:03:07.400 --> 00:03:11.700 the same task without having to concern ourselves with the details of how it 45 00:03:11.700 --> 00:03:17.220 performs the task is central to object oriented programming so another and 46 00:03:17.220 --> 00:03:21.549 hopefully much more healthier example might be an electric kettle now the 47 00:03:21.549 --> 00:03:26.350 imperative approach to boiling water for hot drink might be step 1 fill kettle with 48 00:03:26.350 --> 00:03:26.940 water 49 00:03:26.940 --> 00:03:33.739 step 2 light stove step 3 place kettle on stove step 4 for monitor kettle until 50 00:03:33.739 --> 00:03:38.340 water boils step 5 make the drink now it would then have to be a function that 51 00:03:38.340 --> 00:03:41.989 deals with lighting the stove which might involve further functions to collect 52 00:03:41.989 --> 00:03:47.150 water or coal or was switched on the gas a function to light the match and so on but 53 00:03:47.150 --> 00:03:51.829 in object orientated terms a kettle would have a boil method that is triggered by 54 00:03:51.829 --> 00:03:56.269 operating a switch so the object oriented program comes down to step 55 00:03:56.269 --> 00:03:57.690 one fill kettle with water 56 00:03:57.690 --> 00:04:04.000 step 2 switch kettle on step 3 when kettle switches off make drink so the 57 00:04:04.000 --> 00:04:07.400 imperative approach will also have to identify the type of stove being used as 58 00:04:07.400 --> 00:04:11.660 its futile and probably a bit dangerous to hold a match to electric cooker 59 00:04:11.660 --> 00:04:16.410 now these examples may make it look like we've got something for nothing because 60 00:04:16.410 --> 00:04:20.280 performing the tasks using an object oriented approach involves fewer steps 61 00:04:20.280 --> 00:04:25.340 but there's no magic involved here now the steps are still had to be programmed 62 00:04:25.340 --> 00:04:29.650 but their built into the objects so that anything that using the object doesn't have to be 63 00:04:29.650 --> 00:04:34.340 concerned about them so the object orientated kettle has been built to contain an 64 00:04:34.340 --> 00:04:38.320 element or whatever method it employs could be a ceramic disc or even a small 65 00:04:38.320 --> 00:04:42.970 nuclear reactor and the operation of the heating mechanism has been wired up to 66 00:04:42.970 --> 00:04:47.550 the switch where the kettle was made so a lot of work has gone into the production on 67 00:04:47.550 --> 00:04:51.740 the kettle in other words far more so that the work that goes into making a kettle 68 00:04:51.740 --> 00:04:55.740 for use on the stove but subsequent use of the kettle is there much easier 69 00:04:55.740 --> 00:05:00.389 because it knows how to perform its function itself so one thing that may 70 00:05:00.389 --> 00:05:06.030 not be obvious and I haven't yet mentioned is that everything in Python is an object so Python 71 00:05:06.030 --> 00:05:11.750 is unusual in this respect because even though Python uses objects extensively you 72 00:05:11.750 --> 00:05:15.850 don't actually have to be aware of that to use so Python can be used for 73 00:05:15.850 --> 00:05:20.389 object-oriented programming but can also be used to write purely imperative 74 00:05:20.389 --> 00:05:25.720 programming code so in fact the Wikipedia entry for Python which I'm going to just load 75 00:05:25.720 --> 00:05:33.690 up the screen now you can see the link in the top left hand corner you can see 76 00:05:33.690 --> 00:05:38.960 just down here it says that Python supports multiple programming paradigms 77 00:05:38.960 --> 00:05:43.020 including object oriented imperative and functional programming or procedural 78 00:05:43.020 --> 00:05:48.700 styles so in Java which is considered a class-based object-oriented language 79 00:05:48.700 --> 00:05:51.139 there are primitive types such as an 80 00:05:51.139 --> 00:05:55.440 int and a double so if you've come to Python from Java the following code which I'm 81 00:05:55.440 --> 00:06:01.419 about to show might seem very odd at first so lets just go back to IntelliJ and I've created a 82 00:06:01.419 --> 00:06:06.720 new project here and a new Python file so if you come back come from Java type in 83 00:06:06.720 --> 00:06:17.090 code like this.... 84 00:06:17.090 --> 00:06:26.520 ....so if we just run this see that it does 85 00:06:26.520 --> 00:06:34.440 work and we get the same result each time so line 3...and 86 00:06:34.440 --> 00:06:37.560 that's pretty normal for a programming language like Java 87 00:06:37.560 --> 00:06:44.010 but the code on line 4 would probably be a bit bizarre and that's because the a even though 88 00:06:44.010 --> 00:06:48.430 it's been defined as you can see on line 12 as a number you could see that a has 89 00:06:48.430 --> 00:06:52.550 got an add method even though its an integer so if I command click add 90 00:06:53.580 --> 00:06:58.910 and that is control click on a PC or Linux you can see that there's a method this is the 91 00:06:58.910 --> 00:07:03.970 built-ins definition of the add method and interestingly enough if I go back to oop 92 00:07:03.970 --> 00:07:10.700 our file and click on the plus note that it has gone to the same method in the built ins 93 00:07:10.700 --> 00:07:15.570 so what you can see by that is that whether you use this __add__ 94 00:07:15.570 --> 00:07:20.139 or use a plus it's calling the same code that is built-in 95 00:07:20.139 --> 00:07:25.760 builtins.py file and in this case add two numbers together and smart enough to 96 00:07:25.760 --> 00:07:29.810 figure out the parameters you can see there that the second argument in the 97 00:07:29.810 --> 00:07:35.110 case of line 3 is then added and line 4 the parameters b is added to the value 98 00:07:35.110 --> 00:07:39.940 of a now if this is your first programming language and either you see 99 00:07:39.940 --> 00:07:44.020 nothing at all strange about this or it will make very little sense but don't worry 100 00:07:44.020 --> 00:07:48.450 I'm gonna explain what it all means but I wanted to warn you that you know if 101 00:07:48.450 --> 00:07:53.060 you've come from other programming languages such as Java or C++ you 102 00:07:53.060 --> 00:07:56.820 probably gonna see some aspects of Python that seem surprising to you at 103 00:07:56.820 --> 00:08:00.580 least initially now when I said that everything in Python is an object I meant 104 00:08:00.580 --> 00:08:05.710 everything even types are implemented as classes now I will come 105 00:08:05.710 --> 00:08:09.470 back to that but for now I'm gonna focus on making sense of what I've just done 106 00:08:09.470 --> 00:08:15.620 their so object-oriented programming uses classes and methods to provide object 107 00:08:15.620 --> 00:08:20.680 that encapsulates both data and the functions that operate on that Data now the 108 00:08:20.680 --> 00:08:24.400 method is just another word for function and you have seen me use those terms 109 00:08:24.400 --> 00:08:29.310 interchangeably in this course but being big correct here when a function is part of 110 00:08:29.310 --> 00:08:33.729 a class in Python we call it a method now in Python there is a slight 111 00:08:33.729 --> 00:08:37.690 difference between a function and a method but writing a method is the same 112 00:08:37.690 --> 00:08:42.039 as writing functions now what we gonna do is start with a very simple example 113 00:08:42.039 --> 00:08:47.430 to introduce the concept of a class and show how classes work in Python now this 114 00:08:47.430 --> 00:08:50.290 example be a simple implementation of the electric 115 00:08:50.290 --> 00:08:54.639 kettle so let's go ahead and do that's so close the run window and I'm just gonna 116 00:08:54.639 --> 00:09:01.750 delete this code and we start by creating a kettle class and its gonna model an 117 00:09:01.750 --> 00:09:02.880 electric kettle 118 00:09:02.880 --> 00:09:13.329 so.....and notice how its has been indented for us 119 00:09:13.329 --> 00:09:16.329 automatically and we're going to create a method so.... 120 00:09:17.509 --> 00:09:25.819 .... 121 00:09:25.819 --> 00:09:28.690 ....will talk about this shortly... 122 00:09:28.690 --> 00:09:35.860 we just type in the code for now so... 123 00:09:35.860 --> 00:09:38.860 .... 124 00:09:39.769 --> 00:09:46.440 ...so looking at this it can be helpful to think of a class as a template from 125 00:09:46.440 --> 00:09:51.529 which objects can be created so when we create objects of this kettle class they 126 00:09:51.529 --> 00:09:56.660 all have a name and a price now they won't have the same name nor the same price 127 00:09:56.660 --> 00:10:02.769 each instance of the class will have its own values for name and price so classes 128 00:10:02.769 --> 00:10:07.029 as I said is a template from which objects can be created and all the objects 129 00:10:07.029 --> 00:10:12.040 created from the same class will share the same characteristics now an instance 130 00:10:12.040 --> 00:10:18.139 is just another name for an object created from a class definition so if I now create a 131 00:10:18.139 --> 00:10:23.850 kettle called Kenwood then Kenwood will be an instance of the kettle class you can also 132 00:10:23.850 --> 00:10:28.810 say that kenwood is an object of type kettle so I'm gonna create a couple of 133 00:10:28.810 --> 00:10:33.420 kettle objects so we can see this in practice so let's continue typing and 134 00:10:33.420 --> 00:10:41.560 we will go to line 8 here and type... 135 00:10:41.560 --> 00:11:08.320 .... 136 00:11:08.320 --> 00:11:14.690 ..... 137 00:11:14.690 --> 00:11:19.140 .....and lets make another kettle... 138 00:11:19.140 --> 00:11:31.660 ...and I should really have two lines there 139 00:11:31.660 --> 00:11:36.070 separating the class definition to be consistent with our code so the code on 140 00:11:36.070 --> 00:11:41.660 line 9 creates an instance of the kettle class and we've given it the name 141 00:11:41.660 --> 00:11:46.120 kenwood and other instances is created on line 16 and this time it's called the 142 00:11:46.120 --> 00:11:51.600 Hamilton so each instance has its own values for naming and price and their access 143 00:11:51.600 --> 00:11:56.570 by using . annotations so we type in Kenwood.price or Hamilton.make for 144 00:11:56.570 --> 00:12:02.610 argument sake to access that information so a good analogy here is for plans for 145 00:12:02.610 --> 00:12:07.250 building a house so the plan itself just describes what the house will look like 146 00:12:07.250 --> 00:12:11.710 its size how many rooms it will have etc now this is similar 147 00:12:11.710 --> 00:12:16.430 the plan to the class which is just a template for creating objects once you 148 00:12:16.430 --> 00:12:20.310 have a plan you can then create it as many houses as you want based on that 149 00:12:20.310 --> 00:12:25.540 plan and the same is true of classes once a class has been defined like how 150 00:12:25.540 --> 00:12:30.000 kettle class that we define on line one we can then create as many instances of that 151 00:12:30.000 --> 00:12:34.710 classes as we want now just as you can't do anything with the house plan other than 152 00:12:34.710 --> 00:12:38.700 build houses based on it you can't really do anything with the class other 153 00:12:38.700 --> 00:12:43.730 than create instances of it once the instances have been created you can then call 154 00:12:43.730 --> 00:12:49.580 their methods and access their variables so on line 11 Kenwood.price retrieves 155 00:12:49.580 --> 00:12:54.630 the price from the instance called Kenwood and Hamilton.price will get the 156 00:12:54.630 --> 00:12:59.860 price of the Hamilton instance we can also give the price a new value as we see on line 157 00:12:59.860 --> 00:13:06.850 13 where we set the price to $12.75 so we've been using dot notation extensively in our 158 00:13:06.850 --> 00:13:08.420 programs in this course 159 00:13:08.420 --> 00:13:12.370 when you seen that when we've used the append method to add items to a list for example 160 00:13:12.370 --> 00:13:18.440 also all the TK widgets are objects that have .pack and grid methods amongst 161 00:13:18.440 --> 00:13:23.010 many others to allow them to be placed in the window manager so lets add another 162 00:13:23.010 --> 00:13:27.750 example in this program so I'm going to start typing and put on some code on line 18 163 00:13:27.750 --> 00:13:30.940 gonna type... 164 00:13:42.620 --> 00:13:55.779 ..... 165 00:13:55.779 --> 00:14:08.040 ....and you can see here models kenwood equals$12.75 and Hamilton equals $14.55 so it's 166 00:14:08.040 --> 00:14:12.459 retrieved the values out of each instance of the 2 kettle objects that 167 00:14:12.459 --> 00:14:19.060 we've created so the parameters in the format method are the make price data attributes of 168 00:14:19.060 --> 00:14:23.000 the Kenwood and Hamilton objects another bit of jargon here the 169 00:14:23.000 --> 00:14:27.070 attributes so we've become used to talking about variables but when a 170 00:14:27.070 --> 00:14:31.399 variable is bound to an instance of a class then it's referred to as a data 171 00:14:31.399 --> 00:14:36.110 attribute in Python now their other object-oriented programming terms for 172 00:14:36.110 --> 00:14:40.480 the same things you might find variable attributes also called fields in 173 00:14:40.480 --> 00:14:45.970 languages such as Java or data members in C++ now I'll introduced yet another term 174 00:14:45.970 --> 00:14:49.690 shortly probably in the next video one that's been borrowed from the small talk 175 00:14:49.690 --> 00:14:55.319 language but lets end this video here and in then next video we'll continue our discussion about 176 00:14:55.319 --> 00:14:56.839 objects see you in the next video