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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Inheritance and Composition: A Python OOP Guide (Summary)

Give Feedback

You explored inheritance and composition in Python. You learned about the type of relationships that inheritance and composition create. You also went through a series of exercises to understand how inheritance and composition are implemented in Python.

In this course, you learned how to:

  • Use inheritance to express an is a relationship between two classes
  • Evaluate if inheritance is the right relationship
  • Use multiple inheritance in Python and evaluate Python’s MRO to troubleshoot multiple inheritance problems
  • Extend classes with mixins and reuse their implementation
  • Use composition to express a has a relationship between two classes
  • Provide flexible designs using composition
  • Reuse existing code through policy design based on composition


Sample Code (.zip)

24.2 KB

Here are some books and articles that further explore object oriented design and can be useful to help you understand the correct use of inheritance and composition in Python or other languages:

latedeveloper on April 17, 2020

Superb course; I will be going back to this time and again. I am about a year into my Python journey and this course appeared at just the right moment.

dejiok on April 18, 2020

This is by far the best course on this subject. Excellent, needs going over and over again.

Austin Cepalia RP Team on April 18, 2020

@latedeveloper @dejiok Thank you! This is definitely an advanced course that requires a few watches. If it helps, I’ve just submitted descriptions that summarize each video (they show up beneath the video player), as well as all the code in each video to download. Those should be online soon.

dejiok on April 19, 2020

Hi, Can you please advise if I can re write this section as detailed below. I find it easier to use the class directly to instantiate rather than using the instance attributes. Thanks

      self.productivity = ProductivitySystem()
        self.payroll = PayrollSystem()
        self.employee_addresses = AddressBook()

    def employees(self):
        return [self._create_employee(**data) for data in self._employees]

    def _create_employee(self, id, name, role):
        address = self.employee_addresses.get_employee_address(id)
        employee_role = self.productivity.get_role(role)
        payroll_policy = self.payroll.get_policy(id)
        return Employee(id, name, address, employee_role, payroll_policy)

        ++ I find this easier to read. Would there be something wrong with this approach ++

         def employees(self):
        return [self._create_employee(**data) for data in self._employees]

    def _create_employee(self, id, name, role):
        address = AddressBook().get_employee_address(id)
        employee_role = ProductivitySystem().get_role(role)
        payroll_policy = PayrollSystem().get_policy(id)
        return Employee(id, name, address, employee_role, payroll_policy)

bnik on April 26, 2020

THANK YOU for so much work you put in this course. This is a goldmine for me! It helped me to connect a lot of dots between OPP concepts in Python. General OOP design thinking and Python best practices very much appreciated.

Ronald Fenimore on May 5, 2020

Very good course. I have already gone through twice, the second time I used the pause button frequently and also backup and went back through some sections. I am sure I will be revisiting this course as well as the tutorial. Looking forward to seeing those files for downloading. Thanks for a job well done.

Erikton Konomi on May 18, 2020

Really well done course, Austin! I learned a lot and have a lot more to learn about both methods you described, inheritance and composition. I use the former regularly, but never knew how powerful the latter also could be.

kbram on June 20, 2020

Thanks for a great course, Austin. I learned a ton of new stuff and have to watch at least one more time to pick up the details.

Ghani on Oct. 8, 2020

Great course; many thanks!

geultrin on Nov. 28, 2020

I found this course to be excellent. I must say it seems straightforward watching it, but putting it into practice is much more difficult. As others said, you watch these over and over. I am about to use this information to refactor some automation infrastructure.

taylorchris121 on May 15, 2021

Thankyou, it’s quite comprehensive, very informative!

A great course to really get your teeth into!!

Become a Member to join the conversation.