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

Unlock This Lesson

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

Unlock This Lesson

Introduction to OOP Inheritance

In this video, you’ll learn how inheritance is used to write maintainable Python code that avoids redundancy.

Inheritance allows one class to subclass another. If we want to make the Baby class a child class of the Person class, we can define Baby like this:

class Baby(Person):
    # Baby code here

By default, any Baby object we create will inherit its attributes and methods from its parent. This means it will have the same attributes and members that the parent class defined.

In the video, you’ll see how you can override a method so that the Baby class can contain a different implementation for that method. You’ll also learn how to extend the functionality of the Baby class by defining new methods, which will be exclusive to only Baby objects.

The isinstance() function is used to determine if an object is of a specific type. It takes 2 arguments: the object to be checked, and a type to check against. It supports inheritance, so in the example of our Baby and Person objects, isinstance(baby, Person) will return True because our Baby class is the child class of Person, and so we can say our baby object is a Person too.

Comments & Discussion

Peter T on March 22, 2019

In the above text, please change exampole to example.

Anonymous on March 28, 2019

Lovely tutorial. very clear and concise. but dawg , its 2019. use f strings :)

Eribertt on April 27, 2019

f string +1 lol why can’t we streamline the use of awesome formatting method python 2 is dead soon ๐Ÿ˜€

Become a Member to join the conversation.