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

Inheritance and Composition: A Python OOP Guide (Summary)

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
Download

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:

Avatar image for latedeveloper

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.

Avatar image for dejiok

dejiok on April 18, 2020

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

Avatar image for Austin Cepalia

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.

Avatar image for dejiok

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)
Avatar image for bnik

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.

Avatar image for Ronald Fenimore

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.

Avatar image for Erikton Konomi

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.

Avatar image for kbram

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.

Avatar image for Ghani

Ghani on Oct. 8, 2020

Great course; many thanks!

Avatar image for geultrin

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.

Avatar image for taylorchris121

taylorchris121 on May 15, 2021

Thankyou, it’s quite comprehensive, very informative!

A great course to really get your teeth into!!

Avatar image for madpenguin07

madpenguin07 on Nov. 25, 2022

Awesome course, clear and comprehensive. I will certainly be returning for another view. Thanks for putting the course together.

Avatar image for mikesult

mikesult on Sept. 22, 2023

Great course, I liked your continual redesign and the reasons why. Especially liked the mixin inheritance example and the singleton design examples. Also, I find it instructive when an error is thrown and the solution is shown and fixed on screen. I’m glad that isn’t all edited out. It’s a good educational moment for me see how to deal with the error messages from bugs. Thank for the course, I’m going to come back to repeat it.

Become a Member to join the conversation.