Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Understanding Inheritance in Python

00:00 Inheritance. Just like humans inherit traits from their ancestors, so too do classes in Python. And while human inheritance involves an intractably complex series of processes, thankfully inheritance in object-oriented programming is pretty straightforward.

00:14 Inheritance lets one class acquire the attributes and methods of another. It defines the newly created class as the child class, or subclass, and defines the original class as the parent, or base class.

00:28 This enables you to extend or repurpose existing classes.

00:32 How these traits are inherited does have some nuance though. With inheritance, attributes and methods from the parent class can be inherited directly, where parent behavior or data is used as is.

00:44 They can be overridden, where parent behavior or data is replaced, or they can be extended, where parent behavior or data is augmented or modified. All three types of inheritance can be mixed and matched within a single class and often are. By default though, attributes and methods will be inherited directly.

01:03 Look at this example. You have two classes, Parent and Child. In the Parent, you define the class attribute hair_ color with the value "brown".

01:11 Nothing new here, but look at the class Child.

01:14 The Parent class is passed in the Child class definition with parentheses, like calling Child passing in (Parent).

01:21 Then nothing else is defined in Child. So in this case, Child inherits all of the attributes and methods in Parent.

01:28 The .hair_color attribute on both Parent and Child instances will be "brown".

01:32 Alternatively, you can override an existing attribute or method by defining a new one with the same name, as in this example. Again, the Parent class defines hair_color as "brown", but the Child class defines a .hair_color attribute as well, setting it to "purple". Child overrides .hair_color.

01:50 Because of this, the .hair_color attribute on only Child instances will be "purple". This type of inheritance can be useful when the Child class is mostly the same as the Parent class, requiring some small modification like this.

02:04 If you find yourself overriding a lot of what’s inherited in the Child class, it might actually be a sign that instead of Child inheriting from Parent, Child and Parent should both inherit from a third base class that contains their shared functionality.

02:17 And then we have, in my opinion, the most interesting type of inheritance, which is extension. The built-in super() function lets a class call parent methods and access parent attributes.

02:28 In this example, the class Parent has an __init__() method that creates an attribute named speaks, and assigns it a list with a single string, "English".

02:36 Then Child inherits from Parent, and its __init__() has a couple interesting lines. First, calling super() invokes the parent class, and calling __init__() invokes its initializer.

02:47 This passes the Child class instance into the parent class’s initializer, which gives it the speaks attribute. Then you can call the append() method of the list self.speaks, and add to it the string "German".

03:00 So ultimately the speaks attribute of Child instances will be ["English", "German"].

03:07 So are you ready to try this out with your Starship class? Then hang around for the next lesson.

Become a Member to join the conversation.