Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The ABCs of abstract base classes


In Chapter 6, More Complex Data Types, we looked at the collections module, which offers a number of variations on the mapping theme. These different kinds of collections are built on a foundation of abstract base classes, defined in the collections.abc module. Looking at this module exposes the common features, and the differences, among the collections.

We can see how Sequence is the basis for the built-in tuple class, and MutableSequence is the basis for the built-in list. The Set abstract base class is the basis for the frozenset built-in class, and MutableSet is the basis for the set class. There's no concrete implementation of the Mapping class, but the dict class is the built-in implementation of the MutableMapping class.

If we need to implement a unique kind of collection, one not already provided by the collection module, we're encouraged to use the collections.abc module as a starting point. If we leverage these common base classes, we're assured...