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

Unlock This Lesson

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 Internals: OOP in Python (Summary)

Now you know all about Python’s mechanisms for writing object-oriented code where the data and operations on that data are structured together. You’re an old pro at using the class keyword to create these structures. You can also base the definition of a class on other classes to create hierarchical structures and reuse your code. This mechanism is known as inheritance.

In this course, you’ve learned about:

  • Basic class inheritance
  • Multi-level inheritance, or classes that inherit from classes
  • Classes that inherit directly from more than one class, or multiple inheritance
  • Special methods that you can use when writing classes
  • Abstract base classes for classes that you don’t want to fully implement yet

To continue learning, you can watch the following video courses:

You can also check out the following written tutorials:

This course is the second in a three-part series. Part one is an introduction to class syntax, teaching you how to write a class and use its attributes and methods. Part three dives deeper into the philosophy behind writing good object-oriented code.

Download

Sample Code (.zip)

6.2 KB
Download

Course Slides (.pdf)

1.0 MB

00:00 In the previous lesson, I showed you abstract base classes and a duck typing approach to the sequence protocol. This lesson summarizes part two of the course.

00:11 You can build hierarchies of classes to better represent your real-world structures and to reuse your code. This is known as inheritance. A child class that extends a parent inherits its aspects.

00:25 A child isn’t stuck with those aspects. They can override them. If a child does override the parent, it can use super() to get at the parent’s methods, meaning they don’t have to rewrite them.

00:38 Class hierarchies can have as many levels as you like, and in fact, a class can also inherit from multiple parents. If you want to partially define a class to spec out an interface, the ABC module provides tools for doing that.

00:55 Everything in Python is an object (ka-ching!) and when you do things to that object, like adding, it’s done by invoking a dunder method. For example, the descriptor protocol uses dunder methods to make methods behave like attributes, and the sequence protocol uses dunder methods to make things list-like. The standard library takes full advantage of the class mechanisms in the language, implementing the data class shortcut to make it faster to write attributes and the Enum class to represent groups of constants.

01:30 This is part two of a three-part course. Parts one and two were about the how—the syntax of classes in Python—but just because you can doesn’t necessarily mean you should.

01:43 Part three of the course is a bit more philosophical. It talks about how to write better object-oriented code and maybe when you shouldn’t. Don’t worry, it isn’t highfalutin philosophy.

01:54 There are code examples to show you each of the principles covered. A good part of the course is based on SOLID. That’s an acronym to keep in mind that helps you write better object-oriented code. What’s it stand for? Take part three to find out.

02:09 Or just Google SOLID and object-oriented and spoil the whole thing for yourself.

02:16 Well, thanks for sticking with me so far. On to part three.

mikehillsnc on Sept. 13, 2023

Thanks. I have a better dunderstanding.

Martin Breuss RP Team on Sept. 14, 2023

😂 👏

alnah on Oct. 2, 2023

Once again, thank you for such a great course!

robertportelli on Feb. 23, 2024

The hard rock band AC/DC’s third studio album: Dirty Deeds cheap

Become a Member to join the conversation.