Simplifying complex algorithms with immutable data structures
The concept of a stateful object is a common feature of object-oriented programming. We looked at a number of techniques related to objects and state in Chapter 6, Basics of Classes and Objects, and Chapter 7, More Advanced Class Design. A great deal of the emphasis of object-oriented design is creating methods that mutate an object's state.
We've also looked at some stateful functional programming techniques in the Using stacked generator expressions, Combining map and reduce transformations, and Creating a partial function recipes. We've used types.SimpleNamespace
because it creates a simple, stateful object with easy to use attribute names.
In most of these cases, we've been working with objects that have a Python dict
object that defines the attributes. The one exception is the Optimizing small objects with __slots__ recipe, where the attributes are fixed by the __slots__
attribute definition.
Using a dict
object to store an...