1 00:00:00,000 --> 00:00:03,810 When it comes to using classes in Python, and in most programming 2 00:00:03,820 --> 00:00:07,760 languages, actually, we can inherit other classes, which 3 00:00:07,770 --> 00:00:11,120 means we can use one class as a template for another class 4 00:00:11,130 --> 00:00:13,700 and automatically inherit the goodies. 5 00:00:13,700 --> 00:00:18,600 So we can actually really increase our reusability of any given class. 6 00:00:19,080 --> 00:00:23,470 Now, a good example of this would be a class for 'cats' and 7 00:00:23,480 --> 00:00:28,320 'dogs'. Now, cats and dogs are very different animals, but 8 00:00:28,320 --> 00:00:30,400 they also have a lot of similarities. 9 00:00:30,780 --> 00:00:33,479 For example, they both know how to speak. 10 00:00:33,490 --> 00:00:34,470 They say something. 11 00:00:34,470 --> 00:00:37,000 One says "Meow", one says, "Woof". 12 00:00:37,000 --> 00:00:40,900 They have fur, most of them have fur. 13 00:00:40,910 --> 00:00:42,950 The fur usually has some sort of color. 14 00:00:43,000 --> 00:00:48,600 There are different sub species of cats and different variations of dogs. 15 00:00:48,650 --> 00:00:52,610 And these are data points that sort of come together to create 16 00:00:52,610 --> 00:00:54,600 a base class. 17 00:00:54,600 --> 00:00:57,100 So what we're going to do, is create a class called 'Animal', 18 00:00:57,900 --> 00:01:02,500 and in this class we need to instantiate this class with 19 00:01:02,500 --> 00:01:04,900 specific attributes. 20 00:01:04,900 --> 00:01:07,600 So let's give an animal a 'name'. 21 00:01:07,600 --> 00:01:10,200 Let's say it always needs to have a name, just so, you know, 22 00:01:10,260 --> 00:01:11,390 we can call it something. 23 00:01:11,400 --> 00:01:13,800 And let's just assume we're using pets, not wild animals 24 00:01:13,800 --> 00:01:20,000 here. And every animal needs a 'weight', because if it is a 25 00:01:20,000 --> 00:01:22,300 thing, it probably has some weight. 26 00:01:22,320 --> 00:01:26,550 So 'self.name =', whatever the 'name' is going to be, and 27 00:01:26,560 --> 00:01:28,950 'self.weight =', whatever the 'weight' is going to be. 28 00:01:29,900 --> 00:01:31,600 Now, every animal says something, 29 00:01:31,600 --> 00:01:33,600 so let's give this a 'speak' method. 30 00:01:33,600 --> 00:01:38,600 And this is just going to say, "What does the animal say?" 31 00:01:38,660 --> 00:01:41,000 Because, I mean, it's a generic animal. 32 00:01:41,010 --> 00:01:43,160 We don't know which animal we're talking about here. 33 00:01:43,260 --> 00:01:46,230 We don't even know what the species is yet. 34 00:01:46,240 --> 00:01:48,660 So we just have to implement something very generic. 35 00:01:48,820 --> 00:01:52,880 So we have this generic 'Animal' class, and it comes with two 36 00:01:52,880 --> 00:01:56,200 attributes, a 'name', and a 'weight', and a 'speak' method. 37 00:01:56,200 --> 00:02:01,200 Now let's say we wanted to have a class for 'Cats'. 38 00:02:01,840 --> 00:02:03,880 Well, we could copy and paste this whole thing. 39 00:02:04,000 --> 00:02:11,699 Copy and paste, and the cat says, "Meow", and we could do that. 40 00:02:11,699 --> 00:02:14,710 There's nothing really technically wrong with that, 41 00:02:14,720 --> 00:02:18,400 but there is a nicer way. Because we're already using the 42 00:02:18,410 --> 00:02:19,420 entire '__init__' method, 43 00:02:19,420 --> 00:02:21,300 it's literally the exact same. 44 00:02:21,300 --> 00:02:23,670 What we can do is let's go ahead and get rid of this, 45 00:02:23,670 --> 00:02:26,400 and let's just inherit from 'Animal'. 46 00:02:27,900 --> 00:02:29,700 This 'Animal' matches the name right there. 47 00:02:29,700 --> 00:02:31,800 [no audio] 48 00:02:31,800 --> 00:02:35,700 And so we use 'class', our new class name, an opening parentheses, 49 00:02:35,780 --> 00:02:38,180 the class that we want to extend, 50 00:02:38,180 --> 00:02:44,500 and if we had other classes, we could add other classes as well. We don't, 51 00:02:44,580 --> 00:02:47,480 so we only have the one to inherit from, the closing parentheses 52 00:02:47,480 --> 00:02:49,760 and then a ':', colon. 53 00:02:49,770 --> 00:02:52,160 Now, honestly, at this point in time, we could just say 'pass', 54 00:02:52,190 --> 00:02:55,600 and it's still going to ask us for a 'name' and a 'weight', 55 00:02:55,690 --> 00:02:58,150 and when we do 'speak', it's going to say, "What does the animal say?" 56 00:02:58,150 --> 00:03:03,200 So as an example, let's go ahead and say, 'henry =', 57 00:03:04,200 --> 00:03:06,500 actually, I don't want that to be 'Cats', I want it to be 'Cat'. 58 00:03:06,560 --> 00:03:10,940 'henry = Cat', and it's going to ask for a 'name'. 59 00:03:10,940 --> 00:03:13,800 So the name is going to be "Henry", and his 'weight' is 60 00:03:13,800 --> 00:03:16,000 going to be '9 lbs'. 61 00:03:17,600 --> 00:03:18,600 And we have 'henry'. 62 00:03:19,200 --> 00:03:23,400 We can actually even see that it is technically a 'Cat' class. 63 00:03:24,060 --> 00:03:26,340 Now, in our 'Cat' class, we did nothing. 64 00:03:26,460 --> 00:03:30,190 We just basically said, "Hey, inherit from 'Animal', but just 65 00:03:30,200 --> 00:03:31,270 call it something different". 66 00:03:31,270 --> 00:03:36,300 So this is actually a pointless example, which we'll build on in just a moment, 67 00:03:36,380 --> 00:03:40,460 but this also is a good example in the sense that we have 68 00:03:40,840 --> 00:03:43,510 all the '__init__' stuff within 'Cat' now. 69 00:03:43,750 --> 00:03:46,030 And as an example, let's go ahead and get rid of this and just 70 00:03:46,040 --> 00:03:49,000 try to instantiate the 'Cat' class without 'name', or 'weight', 71 00:03:49,010 --> 00:03:50,230 and see the error that comes up. 72 00:03:51,200 --> 00:03:53,100 We get a 'TypeError: missing 2 required 73 00:03:53,100 --> 00:03:55,000 positional arguments: 'name' and 'weight'. 74 00:03:55,700 --> 00:03:56,700 So let's go ahead, 75 00:03:56,700 --> 00:04:01,400 let's undo that, and let's run 'henry.speak()'. 76 00:04:02,200 --> 00:04:04,500 And 'henry' says, "What does the animal say?" 77 00:04:04,500 --> 00:04:08,400 As far as I know, my little cat cannot speak human. 78 00:04:08,480 --> 00:04:09,680 So we need to change that. 79 00:04:09,690 --> 00:04:12,830 So let's go ahead and get rid of this 'pass', and let's overwrite 80 00:04:12,840 --> 00:04:18,930 'speak'. Because 'Cat' inherits from 'Animal', it automatically 81 00:04:18,930 --> 00:04:19,899 got the 'speak' method. 82 00:04:19,899 --> 00:04:22,300 Now, that 'speak' method is not quite correct. 83 00:04:22,500 --> 00:04:25,399 It's cool that we inherited it, but we needed to actually change it. 84 00:04:25,490 --> 00:04:26,709 So we're going to overwrite this. 85 00:04:26,709 --> 00:04:28,900 And it's going to say, "Meooowwww". 86 00:04:28,900 --> 00:04:31,200 [no audio] 87 00:04:31,200 --> 00:04:35,200 And when we rerun each cell, 'henry' now says, "Meooowwww". 88 00:04:35,750 --> 00:04:38,300 Now to show you the difference here, let's instantiate a 89 00:04:38,300 --> 00:04:39,600 regular 'Animal' class. 90 00:04:40,400 --> 00:04:43,200 So the 'animal = Animal' class, and the 'name' is 91 00:04:43,260 --> 00:04:47,430 "Ani", and the 'weight' is "100 lbs", because whatever animal 92 00:04:47,430 --> 00:04:52,400 this is, it's big one. 'animal.', 'Tab', 'speak'. 93 00:04:52,400 --> 00:04:54,800 And it says, "What does the animal say?" 94 00:04:54,890 --> 00:04:57,020 So this actually has nothing to do with a 'Cat' 95 00:04:57,030 --> 00:05:00,250 right now. The 'Cat' overwrote 'speak'. 96 00:05:00,820 --> 00:05:02,080 It inherited from 'Animals', 97 00:05:02,090 --> 00:05:03,880 so it originally said, "What does the animal say?" 98 00:05:03,890 --> 00:05:05,770 And then we said, "No. No-No, that's not right. 99 00:05:05,770 --> 00:05:07,100 Change it. Change it to, "Meooowwww". 100 00:05:07,140 --> 00:05:09,720 But the 'Animal' class inherits from nothing. 101 00:05:09,730 --> 00:05:11,310 It is just a base class, 102 00:05:11,370 --> 00:05:14,430 and so by default, it has nothing to do with 'Cat'. Does not 103 00:05:14,430 --> 00:05:15,800 really know that it exists. 104 00:05:15,800 --> 00:05:20,000 And by default, it's 'speak' method is going to say, "What does the animal say?" 105 00:05:20,040 --> 00:05:23,850 So now we're actually taking this, and applying this part 106 00:05:23,860 --> 00:05:26,880 inside of it, and overwriting the 'speak' method. 107 00:05:27,900 --> 00:05:29,000 Now, why is this useful? 108 00:05:29,000 --> 00:05:32,200 Well, because we can do the same thing with a 'Dog' class. 109 00:05:32,250 --> 00:05:37,110 'Dog' is going to inherit from an 'Animal', and we can do 'def 110 00:05:37,400 --> 00:05:40,200 speak(): print("Woof woof")'. 111 00:05:40,200 --> 00:05:42,900 [no audio] 112 00:05:42,900 --> 00:05:47,100 And now we can say there is a dog, his name is 'veto'. Dog 113 00:05:47,100 --> 00:05:51,500 his 'name' is "Veto", and his 'weight' is about "120 lbs". 114 00:05:51,500 --> 00:05:53,900 Sure, we'll use an integer there. 115 00:05:55,700 --> 00:06:00,500 'veto.', hit 'Tab', 'speak', 116 00:06:01,500 --> 00:06:02,500 "Woof woof". 117 00:06:03,630 --> 00:06:07,080 And so now what we've done, is we've really reused some of 118 00:06:07,080 --> 00:06:09,900 our code here. We said, 'Animal' is just a base template. 119 00:06:09,900 --> 00:06:11,100 We don't actually need to use it. 120 00:06:11,160 --> 00:06:14,340 But because a 'Cat' and a 'Dog' do have similar things, such as 121 00:06:14,350 --> 00:06:17,330 a 'name' and a 'weight', we don't have to write this inside 122 00:06:17,330 --> 00:06:18,700 of the 'Cat' class. 123 00:06:18,700 --> 00:06:19,800 We don't have to write it in there. 124 00:06:19,800 --> 00:06:22,430 We also don't have to write inside of the 'Dog' class either. 125 00:06:22,500 --> 00:06:25,900 And all we have to do is overwrite the things that we want to change. 126 00:06:26,500 --> 00:06:30,600 It's a lot like cloning, science fiction cloning 127 00:06:30,610 --> 00:06:33,840 anyways, where you have the person that you want to clone, 128 00:06:33,850 --> 00:06:36,360 and then you're going to clone that person, and then 129 00:06:36,370 --> 00:06:38,550 after that person is cloned, they're going to have different life 130 00:06:38,560 --> 00:06:40,710 experiences. And because they have different life experiences, 131 00:06:40,720 --> 00:06:42,870 they are going to eventually become their own 132 00:06:42,870 --> 00:06:45,000 people. They're going to be a little bit different, but they will 133 00:06:45,000 --> 00:06:48,300 still have certain things that are fundamentally the same. 134 00:06:48,300 --> 00:06:55,300 And so this is basically how we clone, in a sense, classes in Python. 135 00:06:55,360 --> 00:06:58,900 Now, what I would like you to do, is create a base class, and 136 00:06:58,910 --> 00:07:03,580 then inherit that class, and instantiate the inherited class 137 00:07:03,670 --> 00:07:05,380 with something that you've overwritten. 138 00:07:05,380 --> 00:07:07,700 Much like what I've done in this video.