1 00:00:00,000 --> 00:00:04,789 [MUSIC] 2 00:00:04,789 --> 00:00:07,465 If there's one programmer stereotype that I generally agree with, 3 00:00:07,465 --> 00:00:09,520 it's that we're a lazy bunch of people. 4 00:00:09,520 --> 00:00:11,710 Now that's not to say that we don't get our work done. 5 00:00:11,710 --> 00:00:14,090 We just don't like to do the same work multiple times, 6 00:00:14,090 --> 00:00:15,810 at least not if we can help it. 7 00:00:15,810 --> 00:00:19,100 We've even turned this mentality into a catchy acronym, DRY or 8 00:00:19,100 --> 00:00:21,030 don't repeat yourself. 9 00:00:21,030 --> 00:00:24,050 Thankfully, a core concept of object-oriented programming is aimed 10 00:00:24,050 --> 00:00:26,920 directly at reducing the amount of repetition you have to do. 11 00:00:26,920 --> 00:00:28,600 It's called inheritance. 12 00:00:28,600 --> 00:00:32,400 At its simplest, it means we can give a child or subclass all of the abilities and 13 00:00:32,400 --> 00:00:35,110 attributes of a parent or super class. 14 00:00:35,110 --> 00:00:38,250 Python actually allows us to inherit from multiple super classes, but 15 00:00:38,250 --> 00:00:39,940 we'll get there when we get there. 16 00:00:39,940 --> 00:00:41,630 For now though, let's get to workspaces and 17 00:00:41,630 --> 00:00:43,760 check out the fastest way to get rich inheritance. 18 00:00:45,130 --> 00:00:47,970 It's really unlikely that we just have thieves in a game, right? 19 00:00:47,970 --> 00:00:51,320 We probably have wizards and warriors and other types. 20 00:00:51,320 --> 00:00:57,060 So let's move most of what we have here out into a Character class. 21 00:00:57,060 --> 00:00:58,810 So, we'll make a new class. 22 00:01:01,620 --> 00:01:06,130 And we'll call this Character, and 23 00:01:06,130 --> 00:01:10,785 we'll say def __init__, self, name and 24 00:01:10,785 --> 00:01:14,859 **kwargs and self.name= name and 25 00:01:14,859 --> 00:01:19,246 for key, value in kwargs.items(). 26 00:01:19,246 --> 00:01:22,716 setattr(self, key, value), okay cool. 27 00:01:22,716 --> 00:01:28,479 So now we'll make our Thief class inherit from Character and 28 00:01:28,479 --> 00:01:35,600 we signify that inheritance by doing these parentheses and a name, okay? 29 00:01:35,600 --> 00:01:40,360 Now we're gonna lose a little bit of our stuff, but we'll bring it back sooner, 30 00:01:40,360 --> 00:01:41,780 I promise. 31 00:01:41,780 --> 00:01:47,880 So we can keep the sneaky, let's take out the init, and 32 00:01:47,880 --> 00:01:50,290 we can even keep the pickpocket and the hide. 33 00:01:50,290 --> 00:01:52,030 Okay, but just for now they're always sneaky. 34 00:01:53,390 --> 00:01:56,030 Okay, so now we have our Thief attributes, 35 00:01:56,030 --> 00:01:59,350 we can't set the sneaky or that kind of stuff. 36 00:01:59,350 --> 00:02:00,870 Now we get back the pickpocket and 37 00:02:00,870 --> 00:02:05,140 the hide because we've defined them in our class. 38 00:02:05,140 --> 00:02:08,330 So, since we have Thief inherit from Character, 39 00:02:08,330 --> 00:02:11,530 Thief gets all the stuff that Character does. 40 00:02:11,530 --> 00:02:16,230 But Thief can add on its own stuff as well, as long as there's no 41 00:02:16,230 --> 00:02:19,440 name collisions, things that are named the same on both of them. 42 00:02:19,440 --> 00:02:22,040 That's fine, they'll live happily next to each other. 43 00:02:22,040 --> 00:02:24,600 And if there are collisions, we'll talk about this a bit more in a bit. 44 00:02:25,780 --> 00:02:28,220 Now, we don't have the ability to set sneakiness anymore, right? 45 00:02:28,220 --> 00:02:29,356 We were doing that through the init. 46 00:02:29,356 --> 00:02:34,840 We'll cover that in the next video cuz it requires a bit more explanation. 47 00:02:34,840 --> 00:02:37,780 For now though, let's just see if our class is working more or 48 00:02:37,780 --> 00:02:39,939 less like it was before, okay. 49 00:02:39,939 --> 00:02:45,330 Save that, pop down here, python from 50 00:02:45,330 --> 00:02:51,450 characters import Thief, kenneth = Thief() with the name of Kenneth. 51 00:02:53,200 --> 00:02:55,910 And if we do kenneth.name, we still get Kenneth. 52 00:02:55,910 --> 00:02:59,636 And if we do kenneth.sneaky, we get True, because that's always set. 53 00:02:59,636 --> 00:03:04,981 Let's do kenneth.sneaky = False and then kenneth.pickpocket. 54 00:03:06,030 --> 00:03:08,290 And we get False, that should still work. 55 00:03:08,290 --> 00:03:10,640 Everything seems to be just like we had it before. 56 00:03:11,880 --> 00:03:14,250 Inheritance can save us a bunch of time? 57 00:03:14,250 --> 00:03:15,030 Something I want to point 58 00:03:15,030 --> 00:03:17,990 out is that we've been using inheritance this whole time. 59 00:03:17,990 --> 00:03:21,350 Every class extends from a built-in class called object. 60 00:03:21,350 --> 00:03:26,300 In legacy Python, you had to specify this inheritance yourself or things got weird. 61 00:03:26,300 --> 00:03:29,880 But with Python 3, the language designers improved the class creation process, so 62 00:03:29,880 --> 00:03:32,720 now everything automatically inherits from object. 63 00:03:32,720 --> 00:03:34,510 All right, time to try a code challenge and 64 00:03:34,510 --> 00:03:36,480 then come back to learn about the super function.