Basics of Inheritance
Learn about the basics of inheritance in Python.
We'll cover the following
In the programming world, duplicate code is considered evil. We should not have multiple copies of the same or similar code in different places. When we fix a bug in one copy and fail to fix the same bug in another copy, we’ve caused no end of problems for ourselves.
There are many ways to merge pieces of code or objects that have similar functionality. In this chapter, we’ll be covering the most famous object-oriented principle: inheritance. Inheritance allows us to create is-a relationships between two or more classes, abstracting common logic into superclasses and extending the superclass with specific details in each subclass. In particular, we’ll be covering the Python syntax and principles for the following:
- Basic inheritance
- Inheriting from built-in types
- Multiple inheritance
- Polymorphism and duck typing
This chapter’s case study will expand on the previous chapter. We’ll leverage the concepts of inheritance and abstraction to look for ways to manage common code in parts of the -nearest neighbors computation.
We’ll start by taking a close look at how inheritance works to factor out common features so we can avoid copy-and-paste programming.
Overview
Technically, every class we create uses inheritance. All Python classes are subclasses of the special built-in class named object
. This class provides a little bit of metadata and a few built-in behaviors, so Python can treat all objects consistently.
If we don’t explicitly inherit from a different class, our classes will automatically inherit from an object
. However, we can redundantly state that our class derives from the object
using the following syntax:
class MySubClass(object):
pass
This is inheritance. In Python 3, all classes automatically inherit from an object
if we don’t explicitly provide a different superclass. The superclasses, or parent classes, in the relationship, are the classes that are being inherited from, the object
in this example. A subclass—MySubClass
, in this example—inherits from a superclass. A subclass is also said to be derived from its parent class, or the subclass extends the parent class.
As you’ve probably figured out from the example, inheritance requires a minimal amount of extra syntax over a basic class definition. Simply include the name of the parent class inside parentheses between the class name and the colon that follows. This is all we have to do to tell Python that the new class should be derived from the given superclass.
Example
How do we apply inheritance in practice? The simplest and most obvious use of inheritance is to add functionality to an existing class.
Parent class
Let’s start with a contact manager that tracks the names and email addresses of several people.
The Contact
class is responsible for maintaining a global list of all contacts ever seen in a class variable, and for initializing the name
and address
of an individual contact:
Get hands-on with 1400+ tech skills courses.