Recall
Here are some of the key points in this chapter:
- Using abstract base class definitions is a way to create class definitions with placeholders. This is a handy technique, and can be somewhat clearer than using
raise NotImplementedError
in unimplemented methods. - ABCs and type hints provide ways to create class definitions. An
ABC
is a type hint that can help to clarify the essential features we need from an object. It’s common, for example, to useIterable[X]
to emphasize that we need one aspect of a class implementation. - The
collections.abc
module defines abstract base classes for Python’s built- in collections. When we want to make our own unique collect class that can integrate seamlessly with Python, we need to start with the definitions from this module. - Creating your own abstract base class leverages the
abc
module. Theabc.ABC
class definition is often a perfect starting point for creating an abstract base class. - The bulk of the work is done by the
type
class. It’s helpful to review this class to understand how classes are created by the methods oftype
. - Python operators are implemented by special methods in classes. We can—in a way—overload an operator by defining appropriate special methods so that the operator works with objects of our unique class.
- Extending built-ins is done via a subclass that modifies the behavior of a built-in type. We’ll often use
super()
to leverage the built-in behavior. - We can implement our own metaclasses to change—in a fundamental way—how Python class objects are built.
Synopsis
In this chapter, we focused on identifying objects, especially objects that are not immediately apparent; objects that manage and control. Objects should have both data and behaviors, but properties can be used to blur the distinction between the two. The DRY principle is an important indicator of code quality, and inheritance and composition can be applied to reduce code duplication.
Get hands-on with 1400+ tech skills courses.