Extending Built-in Functions
Explore how to enhance Python built-in classes by inheritance to add new methods and functionalities. Understand type hinting with generics, extending list and dict classes, and using future annotations to build more specialized object collections.
We'll cover the following...
One interesting use of this kind of inheritance is adding functionality to built-in classes. In the Contact class seen earlier, we are adding contacts to a list of all contacts. What if we also wanted to search that list by name? Well, we could add a method on the Contact class to search it, but it feels like this method actually belongs to the list itself.
Example 1
The following example shows how we can do this using inheritance from a built-in type. In this case, we’re using the list type. We’re going to inform mypy that our list is only of instances of the Contact class by using list["Contact"]. For this syntax
to work in Python, we need to also import the annotations module from the __future__ package. The definitions look like this:
Extend superclass’ list
Instead of instantiating a generic list as our class variable, we create a
new ContactList class that extends the built-in list data type. Then, we ...