Contribute Media
A thank you to everyone who makes this possible: Read More

Set Practice: learning from Python's set types

Description

Key takeaways:

  1. Set operations enable simpler and faster solutions for many tasks;
  2. Python's set classes are lessons in elegant, idiomatic API design;
  3. A set class is a suitable context for implementing operator overloading.

Boolean logic and set theory are closely related. In practice, we will see cases where set operations provide simple and fast declarative solutions to programming problems that otherwise require complicated and slow procedural coding.

Python's set built-ins and ABCs provide a rich and well designed API. We will consider their interfaces, and how they can inspire the creation of Pythonic APIs for your own classes.

Finally, we will discuss operator overloading — a technique that is not suitable everywhere, but certainly makes sense with sets. Taking a few operators as examples, we will study their implementation in a new UintSet class for integer elements. UintSet fully implements the MutableSet interface over a totally different internal representation based on a bit array instead of a hash table. Membership tests run in O(1) time like the built-in sets (however, UintSet is currently pure Python, so YMMV). Using bit arrays allow core set operations like intersection and union to be implemented with fast bitwise operators, and provides compact storage for dense sets of integers.

Improve this page