Designing classes with lots of processing
Some of the time, an object will contain all of the data that defines its internal state. There are cases, however, where a class doesn't hold the data, but instead is designed to consolidate processing for data held in separate containers.
Some prime examples of this design are statistical algorithms, which are often outside the data being analyzed. The data might be in a built-in list
or Counter
object; the processing defined in a class separate from the data container.
In Python, we have to make a design choice between a module and a class. A number of related operations can be implemented using a module with many functions. See Chapter 3, Function Definitions, for more information on this.
A class definition can be an alternative to a module with a number of functions. How can we design a class that makes use of Python's sophisticated built-in collections as separate objects?
Getting ready
In Chapter 4, Built...