Inheritance and Composition: A Python OOP Guide (Overview)

In this course, you’ll explore inheritance and composition in Python. Inheritance and composition are two important concepts in object oriented programming that model the relationship between two classes. They are the building blocks of object oriented design, and they help programmers to write reusable code.

By the end of this course, you’ll know how to:

  • Use inheritance in Python
  • Model class hierarchies using inheritance
  • Use multiple inheritance in Python and understand its drawbacks
  • Use composition to create complex objects
  • Reuse existing code by applying composition
  • Change application behavior at run-time through composition

Sample Code (.zip)

24.2 KB

00:00 Hi! My name is Austin Cepalia with, and I welcome you to the next stepping stone on your path to learning object-oriented programming in Python 3. This course covers inheritance and composition, two major ideas that will allow you to write better object-oriented Python code.

00:21 By the end of this course, you will know how to create classes that inherit from one another, design basic class hierarchies with UML diagrams, use interfaces and abstract classes, use multiple inheritance and understand its drawbacks, build complex classes with composition, and finally, change run-time behavior using composition.

00:47 So sit back and relax, because this is a big course. Before you continue, it’s important that you understand the basic ideas behind object-oriented programming in Python.

01:00 That means that you should have already gone through my first Real Python course, Intro to Object-Oriented Programming in Python. In that course, I introduce you to the fundamental ideas behind OOP, and I demystify buzzwords like class, object, instance, attribute, and method. If those words sound foreign to you, I’d recommend taking another pass through that course before continuing.

01:29 Optionally, you can also read the article on the right, which teaches you about the Python super() function.

01:37 And finally, just a little bit of advice. This is a long course, and unless you already understand these concepts and you’re watching as a refresher, then you really shouldn’t rush through this in one sitting.

01:50 Believe it or not, your brain works hard to understand a concept even when you’ve stepped away from the computer after a learning session. That’s why it’s so important to take breaks.

02:02 Give yourself time to process a few ideas before you try to learn a bunch more. The best way to learn something that seems big and daunting is to take it bit by bit—no pun intended.

02:14 That’s one reason why every video course and article on is organized into sections. Watch the videos, code along with me, and solidify your understanding of the material by seeing how small modifications to the code you write changes the output of the program.

02:34 I can tell you as an undergraduate student studying computer science that this is how you effectively learn to program. Let’s get started.

patientwriter on April 30, 2020

Can you tell me why Django, at least in the official docs, spends so much space talking about inheritance and almost none talking about composition?

Become a Member to join the conversation.