Book Image

Mastering Object-oriented Python

By : Steven F. Lott, Steven F. Lott
Book Image

Mastering Object-oriented Python

By: Steven F. Lott, Steven F. Lott

Overview of this book

Table of Contents (26 chapters)
Mastering Object-oriented Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Some Preliminaries
Index

ABCs of collections


The collections.abc module provides a wealth of abstract base classes that decompose collections into a number of discrete feature sets.

We can successfully use the list class without thinking too deeply about the various features and how they relate to the set class or the dict class. Once we start looking at the ABCs, however, we can see that there's a bit of subtlety to these classes. By decomposing the aspects of each collection, we can see areas of overlapping that manifest themselves as an elegant polymorphism even among different data structures.

At the bottom of the base classes are some "one-trick pony" definitions. These are the base classes that require a single special method:

  • The Container base class requires the concrete class to implement the __contains__() method. This special method implements the in operator.

  • The Iterable base class requires __iter__(). This special method is used by the for statement and the generator expressions as well as the iter() function...