From the course: Python Essential Training

Inheritance - Python Tutorial

From the course: Python Essential Training

Inheritance

- My child, one day, all of this will be yours. Your inheritance. (ethereal music) So you see, in computer science and in Python, a class can extend another class. The original class is called the parent class, and the class extending it is the child class. And the child class inherits all of the parent classes' methods and attributes. But this inheritance happens automatically when the child class is created. Let's go to the code. - Yeah. - [Instructor] Here, we have the dog class that we used previously, and say we want to use this to make a chihuahua class, which is a type of small dog. Here's how we can do that. Class Chihuahua dog. And let's just add pass for now. We use these parentheses after the class name to indicate that this class is extending the parent dog class. And for now, let's just not add anything else. Just write pass to get this class in place. So we can use this to create a new Chihuahua, and you can see that it has all of the attributes and methods of the parent dog class. So Chihuahua, let's call it Roxy, and dog dot speak. Hmm, that doesn't seem quite right. Have you ever actually heard a chihuahua say bark? Let's fix this. Let's extend this method, print. Let's use an f-string for this. Self dot name says yap, yap, yap. Okay. Much better. If the child class, chihuahua, defines any attributes or methods that conflict with the parent class, the parent class's methods get overwritten, and the child class method is used instead. So you can see that we can make an actual dog still. My dog equals dog, Rover, my dog dot speak, and that still says bark. We can also add additional methods to the child class. For example, we can do this, wag tail, print vigorous wagging. Okay, so now our chihuahua should have a wagtail method. Wagtail. Great. So this can be extremely useful if, for example, there's a class that you really want to use, but it just needs a couple tweaks or maybe an extra method or something like that. We can even do this with Python's own built-in classes. Remember, in Python, you can instantiate a new list by calling it like this. My list equals list, okay? And even though this is lowercase, and it looks like a function, list is actually a class. So let's say we want to make a list that guarantees that all of the items appended to it are unique. A bit like a set. We can extend the list class and make our own unique list class. Our unique list extends list, okay. Then we're going to override the append function. Append self item, if item in self, return self dot append item. Okay, but wait, not so fast. We're calling self dot append, which is going to call this exact same function right here. This is self dot append, and this is going to lead to infinite recursion or a never ending loop and break our program. What we actually want to do is call append in the parent class, the original list class. In this case, we can use a function called super, which gets the underlying instance of the parent class, and we'll call super dot append. So let's try this out. Unique list is equal to unique list. So we're creating a new list. Let's do unique list dot append one, append one, append two, and print unique list. And you can see that even though we added one twice, it only recorded it once. There's also another really common use case where super is used, and that's in the constructor. So, say you want to add another attribute to your child class instance, we could do something like this, init self, self dot some property equals unique list. Okay, so the problem is that we're completely overwriting the constructor and the parent class now, where it may have some really important stuff that it needs to initialize. We can solve this by using super again. Init, okay, and this makes sure that the parent constructor gets called first, and then we add our new property, and we can see that when we instantiate this. Let's print out some property. There we go. Class extensions might look complicated at first but it's an incredibly elegant and powerful tool that can solve a lot of really tricky coding problems. You certainly don't need an estate lawyer to figure out inheritance.

Contents