Complex Relationships3:37 with Kenneth Love
Classes are great, but they're even better with parents.
Inheritance in Python is both easier and harder than in some other languages. Python's inheritance usually works exactly how you expect it to. The child classes, or subclasses, get all of the attributes and methods of their parents and grandparents and so on. But thanks to things like the "method resolution order (MRO)", which we'll talk about later, sometimes it's not that simple. But let's not worry about that just yet! Make sure you understand these terms before moving on:
- Parent or Super class: the class that a class inherits from. These can go on for a long way, too, so be sure to consider grandparent and great-grandparent classes, too.
All classes have the ultimate ancestor of
- Child or Sub class: the class that inherits from a particular class.
class Symbol: pass class Letter(Symbol): pass class Alpha(Letter): pass
Letter has two superclasses:
Symbol inherits from
object even though we didn't explicitly state it).
Letter has one subclass,
An instance of the class
Alpha could use any attributes or methods that were defined on
Letter. Those two classes, though, wouldn't have access to attributes or methods that belonged to
Alpha. Inheritance is a one-way street.
[MUSIC] 0:00 If there's one programmer stereotype that I generally agree with, 0:04 it's that we're a lazy bunch of people. 0:07 Now that's not to say that we don't get our work done. 0:09 We just don't like to do the same work multiple times, 0:11 at least not if we can help it. 0:14 We've even turned this mentality into a catchy acronym, DRY or 0:15 don't repeat yourself. 0:19 Thankfully, a core concept of object-oriented programming is aimed 0:21 directly at reducing the amount of repetition you have to do. 0:24 It's called inheritance. 0:26 At its simplest, it means we can give a child or subclass all of the abilities and 0:28 attributes of a parent or super class. 0:32 Python actually allows us to inherit from multiple super classes, but 0:35 we'll get there when we get there. 0:38 For now though, let's get to workspaces and 0:39 check out the fastest way to get rich inheritance. 0:41 It's really unlikely that we don't have thieves in a game, right? 0:45 We probably have wizards and warriors and other types. 0:47 So let's move most of what we have here out into a character class. 0:51 So, we'll make a new class. 0:57 And we'll call this character, and 1:01 we'll say definite, self, name and 1:06 kwargs and self.name= name and 1:10 for key, value in kwargs.items. 1:14 Setattr self key value, okay cool. 1:19 So now we'll make our Thief class inherit from character and 1:22 we signify that inheritance by doing these parentheses and a name, okay? 1:28 Now we're gonna lose a little bit of our stuff, but we'll bring it back sooner, 1:35 I promise. 1:40 So we can keep the sneaky, let's take out the init, and 1:41 we can even keep the pickpocket and the hide. 1:47 Okay, but just for now they're always sneaky. 1:50 Okay, so now we have our thief attribute, 1:53 we can't set the sneaky or that kind of stuff. 1:56 Now we get back the pickpocket and 1:59 the hide because we've defined them in our class. 2:00 So, since we have thief inherit from character, 2:05 Thief gets all the stuff that character does. 2:08 But Thief can add on its own stuff as well, as long as there's no 2:11 name collisions, things that are named the same on both of them. 2:16 That's fine, they'll live happily next to each other. 2:19 And if there are collisions, we'll talk about this a bit more in a bit. 2:22 Now, we don't have the ability to set sneakiness anymore, right? 2:25 We were doing that through the init. 2:28 We'll cover that in the next video cuz it requires a bit more explanation. 2:29 For now though, let's just see if our class is working more or 2:34 less like it was before, okay. 2:37 Save that, pop down here, python from 2:39 characters import Thief, kenneth = Thief with the name of Kenneth. 2:45 And if we do kenneth.name, we still get Kenneth. 2:53 And if we do kenneth.sneaky, we get True, because that's always set. 2:55 Let's do kenneth.sneaky = False and then kenneth.pickpocket. 2:59 And we get False, that should still work. 3:06 Everything seems to be just like we had it before. 3:08 Inheritance can save us a bunch of time? 3:11 Something I want to point 3:14 out is that we've been using inheritance this whole time. 3:15 Every class extends from a built-in class called object. 3:17 In legacy Python, you had to specify this inheritance yourself or things got weird. 3:21 But with Python 3, the language designers improved the class creation process, so 3:26 now everything automatically inherits from object. 3:29 All right, time to try a code challenge and 3:32 then come back to learn about the super function. 3:34
You need to sign up for Treehouse in order to download course files.Sign up