1 00:00:00,000 --> 00:00:03,270 Okay, let's take a look at a real life Object Oriented Programming 2 00:00:03,270 --> 00:00:07,300 example. So up until this point, we've learned about classes, 3 00:00:07,300 --> 00:00:10,300 class attributes, class methods, and sort of the 'self' keyword, 4 00:00:10,380 --> 00:00:12,530 and all sorts of stuff behind the scenes there. 5 00:00:12,530 --> 00:00:15,250 But we actually haven't used any real example. 6 00:00:15,260 --> 00:00:18,580 So let's use an example where, let's say maybe we have a service 7 00:00:18,600 --> 00:00:21,500 where we are working with people's pets. 8 00:00:22,200 --> 00:00:24,500 And so we have different types of animals here. 9 00:00:24,500 --> 00:00:27,500 So we can say 'class Animal'. 10 00:00:28,010 --> 00:00:33,229 And in here we can use '__init__', the magic method 11 00:00:33,229 --> 00:00:34,500 '__init__', always takes 'self'. 12 00:00:34,500 --> 00:00:36,400 And what 'type' of animal is it? 13 00:00:36,400 --> 00:00:38,600 What is the animal's 'name'? 14 00:00:38,600 --> 00:00:43,400 And instead of 'type', because that is actually a word, let's 15 00:00:43,400 --> 00:00:46,200 use 'species'. Let's ask for a 'name'. 16 00:00:46,200 --> 00:00:48,700 And I don't know, let's give it a 'weight'. 17 00:00:48,700 --> 00:00:50,900 And so we can say 'self.species 18 00:00:50,900 --> 00:00:56,200 = species', 'self.name = name', 19 00:00:56,200 --> 00:00:58,700 and 'self.weight = weight'. 20 00:00:58,700 --> 00:01:02,090 Then because every animal can speak, it's going to say something, so 21 00:01:02,100 --> 00:01:04,800 'def speak(self)', 22 00:01:04,849 --> 00:01:07,460 and because I don't know what this animal is, it's just going 23 00:01:07,470 --> 00:01:09,500 to print a grunting sound. 24 00:01:09,500 --> 00:01:12,700 I guess. Most animals know how to grunt, right? 25 00:01:13,600 --> 00:01:14,600 And so let's save that. 26 00:01:14,600 --> 00:01:16,700 And let's create a new animal here. 27 00:01:16,700 --> 00:01:17,870 So I have an animal. 28 00:01:17,870 --> 00:01:19,400 His name is "zephyr". 29 00:01:19,400 --> 00:01:21,500 He's going to be a new 'Animal()' class. 30 00:01:22,500 --> 00:01:27,500 He is a house "Cat", his name is "Zephyr", and his weight is 31 00:01:27,500 --> 00:01:29,500 "14.5 lbs". 32 00:01:29,500 --> 00:01:30,800 He is a larger cat. 33 00:01:31,500 --> 00:01:32,500 I also have another cat. 34 00:01:32,550 --> 00:01:35,280 His name is Henry animal cat. 35 00:01:35,480 --> 00:01:39,810 His name is "Henry", and he's a little bit smaller. 36 00:01:39,810 --> 00:01:40,800 He is "9 lbs". 37 00:01:40,840 --> 00:01:44,110 So what we've done here is, we've written a template for an 38 00:01:44,110 --> 00:01:47,400 'Animal' that's always going to have a 'species', a 'name', and a 'weight'. 39 00:01:47,810 --> 00:01:52,080 And without having to rewrite 'species', 'name', and 'weight' 40 00:01:52,080 --> 00:01:55,400 every single time, we wrote out this template, and then we 41 00:01:55,400 --> 00:01:59,700 instantiated a new 'Animal' class, and we specified 42 00:01:59,700 --> 00:02:01,000 the 'species', 'name' and 'weight'. 43 00:02:01,000 --> 00:02:05,200 And the logic that happens behind the scenes is already done for us. 44 00:02:05,200 --> 00:02:08,600 So at any point in time, I can say, 'zephyr.speak()', 45 00:02:08,600 --> 00:02:12,000 I can also say 'henry.speak()', 46 00:02:12,000 --> 00:02:14,000 [no audio] 47 00:02:14,000 --> 00:02:16,400 and so with just one line of code for each one of these, 48 00:02:16,450 --> 00:02:20,510 I have now effectively used nine lines of code behind the 49 00:02:20,520 --> 00:02:23,990 scenes. Now, if you're still not convinced on why Object 50 00:02:24,000 --> 00:02:27,000 Oriented Programming is useful, without it we would have to 51 00:02:27,000 --> 00:02:28,700 do one of two methods. 52 00:02:28,700 --> 00:02:31,000 The first method is really gross. 53 00:02:31,000 --> 00:02:32,600 It uses a lot of variables. 54 00:02:32,600 --> 00:02:36,700 So 'zephyr_name = "Zephyr"'. 55 00:02:36,700 --> 00:02:39,700 'zephyr_species = "Cat"'. 56 00:02:40,000 --> 00:02:45,500 'zephyr_weight = "14.5 lbs"'. 57 00:02:45,800 --> 00:02:47,600 Okay, not bad, not bad, 58 00:02:47,600 --> 00:02:50,000 but now I also need to specify "henry". 59 00:02:50,000 --> 00:02:54,000 So, 'henry_name = "Henry"'. 60 00:02:54,050 --> 00:02:55,620 'henry_species =', 61 00:02:55,620 --> 00:02:58,300 like you get the point here, I'd have to write this out for 62 00:02:58,300 --> 00:03:00,000 every single animal. 63 00:03:00,000 --> 00:03:05,200 Now, the thing is down the road is, what if I wanted to get 64 00:03:05,200 --> 00:03:06,200 Zephyr's weight? 65 00:03:06,200 --> 00:03:09,800 But let's say this was accidentally not set. 66 00:03:09,840 --> 00:03:11,280 How do I get his weight? 67 00:03:11,290 --> 00:03:14,280 Well, I have to check to see if that variable even exists, 68 00:03:14,280 --> 00:03:17,400 and then if that variable does exist, it is very specifically 69 00:03:17,400 --> 00:03:18,500 just Zephyr. 70 00:03:18,500 --> 00:03:20,900 It's not Henry, it's not anyone else. 71 00:03:20,900 --> 00:03:23,050 This variable is literally just for Zephyr. 72 00:03:23,060 --> 00:03:27,010 So the reusability of this variable is a lot lower than if 73 00:03:27,010 --> 00:03:28,500 we used it in a class. 74 00:03:28,500 --> 00:03:30,500 [no audio] 75 00:03:30,500 --> 00:03:34,700 The second option is, we can say 'zephyr' is an object or not an 76 00:03:34,700 --> 00:03:36,200 object, but a dictionary. 77 00:03:36,280 --> 00:03:41,170 And then we could say 'name': "Zephyr", 78 00:03:42,500 --> 00:03:44,900 'species': "Cat". 79 00:03:45,710 --> 00:03:48,990 'weight': "14.5 lbs". 80 00:03:49,740 --> 00:03:52,290 Now, this is actually not a terrible way of doing things. 81 00:03:53,200 --> 00:03:57,100 However, if I wanted to also do the same thing about "Henry", 82 00:03:57,150 --> 00:04:01,050 I would have to copy this over and change his name to "Henry", 83 00:04:01,080 --> 00:04:03,570 which, again, not a terrible thing, but copy and paste can 84 00:04:03,580 --> 00:04:05,280 be dangerous because we tend to forget things. 85 00:04:06,000 --> 00:04:07,300 So he's "9 lbs". 86 00:04:07,300 --> 00:04:08,470 We can set that as well. 87 00:04:08,480 --> 00:04:10,030 So that's a little bit easier. 88 00:04:10,030 --> 00:04:15,030 But let's say you have Zephyr, Henry, Ted, Ezra, and maybe 89 00:04:15,040 --> 00:04:16,440 like a pet lizard or something. 90 00:04:16,450 --> 00:04:18,630 And you have to do this for every single pet. 91 00:04:18,860 --> 00:04:22,880 Well, what if down the road you were changing 'weight' from 92 00:04:23,000 --> 00:04:27,320 pounds to kilograms, you would have to find every single 93 00:04:27,329 --> 00:04:30,530 instance of every single animal, which means you have to 94 00:04:30,540 --> 00:04:32,930 manually scan your code because there's only one reference 95 00:04:32,930 --> 00:04:33,900 to 'zephyr' here, 96 00:04:33,900 --> 00:04:36,300 there's only one reference to 'henry' here, well, there's two, 97 00:04:36,380 --> 00:04:39,290 but in a variable name, there's only the one reference. 98 00:04:39,950 --> 00:04:44,020 Or if you use the class, you could add some logic in here. 99 00:04:45,010 --> 00:04:55,360 And you could say 'if weight.endswith("lbs"): "#Convert to 100 00:04:55,360 --> 00:05:00,300 kilograms". And then you could say 'self.weight = 101 00:05:00,300 --> 00:05:02,700 whatever the weight is going to be in kilos. 102 00:05:02,780 --> 00:05:05,900 And now you've effectively updated all of your code. 103 00:05:05,910 --> 00:05:09,500 So even if someone puts in "14.5 lbs", it's going to say 104 00:05:09,500 --> 00:05:10,800 it ends with "lbs", 105 00:05:11,800 --> 00:05:14,800 and it's going to convert that weight to kilograms. 106 00:05:14,860 --> 00:05:17,950 And not only do you only have to do it in the one place, 107 00:05:17,950 --> 00:05:22,400 you no longer need to do a search and find for 'zephyr', 'henry', 108 00:05:22,480 --> 00:05:24,070 'ted', 'ezra', your pet lizard. 109 00:05:24,080 --> 00:05:26,110 You don't have to do it for every single one. 110 00:05:26,300 --> 00:05:30,680 And in the variable example, you don't have to go ahead and 111 00:05:30,690 --> 00:05:36,080 change this from "14.5 lbs" to "14.5 kilos" or kilograms, and 112 00:05:36,080 --> 00:05:39,500 do the math behind it, because your program will do that for you. 113 00:05:40,800 --> 00:05:44,600 That is a very, very good reason and a good real life example 114 00:05:44,620 --> 00:05:47,740 of why we would want to use Object Oriented Programming. 115 00:05:47,740 --> 00:05:51,200 It is a nice template for repeatable code. 116 00:05:51,200 --> 00:05:52,266 [no audio]