Using the standard library extensions

We'll look at some extensions to built-in classes that are already part of the standard library. These are the collections that extend or modify the built-in collections. Most of these are covered in one form or another in books such as Python 3 Object-Oriented Programming - Third Edition by Dusty Phillips.

We'll look at the following four collection from this library:

  • deque (note the atypical class name) is a double-ended queue, a list-like collection that can perform fast appends and pops on either end. A subset of the features of this class will create single-ended stacks or queues.
  • ChainMap is a view of multiple mappings. Instead of merging mappings together, we can keep them separate and chain among them to locate which mapping contains a requested key. 
  • defaultdict (note the atypical spelling) is a dict subclass that uses a factory function to provide values for missing keys.
  • Counter is a dict subclass that can be used for counting objects to create frequency tables. However, it's actually a more sophisticated data structure called a multiset or bag.

There are two collections in this library that have been replaced by more advanced versions:

  • The namedtuple() function creates a subclass of tuple with named attributes. This has been replaced by the NamedTuple definition in the typing module. We'll emphasize the new typing.NamedTuple class because it permits type hints. The legacy function is no longer useful.
  • An OrderedDict collection is a mapping in which the original key entry order is maintained. This feature of maintaining key order is now a first-class part of the built-in dict class, so this special collection isn't necessary anymore.

We'll see examples of the preceding collection classes. There are two important lessons to be learned from studying the library collections:

  • What features are already present the standard library; this will save us from reinvention
  • How to extend the ABCs to add interesting and useful structures to the language

Also, it can be helpful to read the source for the libraries. The source will show us numerous Python object-oriented programming techniques. Beyond these basics are even more modules. They are as follows:

  • The heapq module is a set of functions that impose a heap queue structure on an existing list object. The heap queue invariant is the set of those items in the heap that are maintained, in order to allow rapid retrieval in ascending order. If we use the heapq methods on a list structure, we will never have to explicitly sort the list. This can have significant performance improvements.
  • The array module is a kind of sequence that optimizes storage for certain kinds of values. This provides list-like features over potentially large collections of simple values.

We won't provide detailed examples of these advanced modules. In addition, of course, there's the deeper computer science that supports these various data structure definitions.

Let's take a look at the different classes in the next few sections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset