Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

OOP Inheritance

These lessons are part of a Real Python video course by Austin Cepalia. You’ve already watched the first few lessons in this video course earlier this week.

00:00 Welcome back to our series on object-oriented programming in Python. In the last video, we learned about instance methods, which are used to represent the behaviors our objects have. This whole idea of classes, creating objects—little bundles of associated data and actions—is a really powerful thing, but it’s also susceptible to a bad coding practice called DRY. DRY stands for Don’t Repeat Yourself. This says that when we write code, we should avoid copying and pasting the same chunks of code whenever possible.

00:33 If you’re writing a nontrivial Python program and you find yourself copying and pasting attributes and instance methods between a lot of different classes, that means that it’s probably time to rethink how you’re designing your software. Take a look at this example.

00:48 Let’s say we are writing a program that’s supposed to construct and maintain lots of different people. As you can probably guess, each person is going to be an object—but here’s the caveat: we can have general people objects, and then we can have a special type of person called a baby.

01:06 A baby can do everything a regular person can do, except it might do some things a little bit differently, like talking. In addition, it might also have some unique behaviors that our general person doesn’t have, such as the ability to take a nap whenever it wants. Being a baby sounds great.

01:25 As you might have guessed, we’re going to need two different classes here: one for the person, and then one for the baby. You’ll notice here that both of these classes are the exact same, but the Baby class has one more behavior called .nap().

01:40 Now, we could design our software this way and it would work fine, but remember, a baby has to be able to do everything a more general person can do, albeit a little bit differently sometimes. This means that our Baby class is going to have the same instance methods that the Person class has, and most of those methods are going to look the exact same,

02:01 But now, what happens if we want to make a change to the Person class? If we change the .action()` method from something like jumping to spinning, then we also have to change the baby’s .action() method as well.

02:16 And if we add other classes, like Adult or Teenager, then we’d also have to maintain those, too. Copying code might make things easier in the moment, but it can make our software really difficult to maintain later on, especially as it grows. To solve this problem, let’s take a step back and think about the relationships that exist between these two classes.

02:37 I think that you would agree that a baby is a person, right? That’s why it shares the same behaviors as the Person class. So, what if there was a way that we can make the Baby class automatically inherit every attribute and instance method from the Person, but then modify them a little bit to fit the Baby class’ needs.

02:58 Then, we could even add our own behavior, like .nap(), that would be exclusive to the Baby class. This right here is called object inheritance, and if used right, it makes managing lots of similar classes a lot easier.

03:14 Inheritance allows us to make one class the child class, or the derived class, of another class called the parent class, or the base class. In Python, the child class automatically inherits every attribute and instance method from the parent class, but we can also redefine certain attributes or methods.

03:35 So, if we want our Baby object to speak differently than a Person object would, we can do that. If we want our Baby object to have the ability to nap, we can do that too. And if we need to make our person spin, instead of jump, we can modify the Person class and the Baby class will automatically pick up those changes.

03:55 There’s no need to edit code in more than one place. To understand inheritance, take a look at this chart on the right. At the very top of the chart is object. In

04:06 many languages, Python included, object is the parent class of all classes in the language. This includes built-in classes, like the int or the str, as well as any custom classes we create. In this diagram, you can see that Baby inherits from Person, which in turn inherits from object.

04:26 As we’ll see later on, Python sees any Baby object we create as also a Person and an object.

You must own this product to join the conversation.