Inheritance and Internals: OOP in Python (Overview)
Python includes mechanisms for writing object-oriented code where the
data and operations on that data are structured together. The class
keyword
is how you create these structures in Python. The definition of a class can be
based on other classes, allowing the creation of hierarchical structures and
promoting code reuse. This mechanism is known as inheritance.
In this course, you’ll learn 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
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.
00:00 Welcome to Inheritance and Internals: Object-Oriented Coding in Python. My name is Christopher, and I will be your guide. This is part two of a multi-part course, an opus on object-oriented coding.
00:15
Part one covered the class
keyword and how you use it to describe classes which are instantiated as objects in Python. It talked about attributes and methods, including the difference between those that operate at the class and object levels.
00:29 It also introduced the descriptor protocol, showing how you can use the property and setter decorators to make a method behave like an attribute.
00:38 This is part two. It primarily focuses on inheritance, the different ways you can base a class on other classes in order to create hierarchical data relationships and reuse code. Along the way, there will be a bit more coverage of some of those magical special methods called dunder methods and attributes, showing you more techniques you can use to change how classes operate in Python.
01:02 Depending on when you’re taking this course, part three may or may not be released yet. Part three covers more of the philosophy of object-oriented coding and how to write and design better classes. A link to part one is in the notes below, and as soon as part three is released, a link for that will be there as well.
01:20 This part is primarily about inheritance. In it, you’ll learn about inheritance, multi-level inheritance, multiple inheritance, more special methods, some of the classes in the standard library and how they take advantage of these features, and abstract base classes.
01:40 The code in this course was tested with Python 3.11. Object-oriented coding in Python has gone through some changes over the years, and so the code presented here won’t work in Python 2 without some changes.
01:52 For the most part, I’ve stuck with general Python 3. There is the odd line here or there that uses newer language features, though I’ll point those out when they occur.
02:03 Part one of the course showed you the syntax used to declare a class in Python and how the different kinds of attributes and methods work. When you declare a class, you can make it based on another class.
02:15 This is called inheritance, amongst other things, and it’s used to build classes that match a real-world hierarchy or to create code reuse. Everything in a parent class is available in a child class that inherits from it, meaning you don’t have to write it twice.
02:32 Inheritance isn’t limited to a single level. You can have grandkids, great-grandkids, and more—as many levels as meet your needs. Not only that, but classes can have multiple parents—more than two, even—allowing you to gain the functionality of several immediate ancestors. Additionally, there are times when you know what a class should look like, but want to leave the implementation details to an inheritor. To do this, you can partially implement a class. This is known as an abstract class.
03:03 The interpreter enforces the implementation. If the child class, sometimes known as a concrete class, doesn’t implement everything, Python raises an error.
03:13
All this fancy object-oriented coding isn’t just for you. It’s been taken advantage of in the standard library. The data class was introduced in Python 3.7 as a shortcut for creating classes that are mainly about their attributes, and the Enum
, a structure that is often a keyword in other languages, has been built as a class. Let’s get started. Next up, I’ll dive into inheritance.
Become a Member to join the conversation.