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 7, Basics of Classes and Objects, and Chapter 8, More Advanced Class Design. A great deal of the emphasis of object-oriented design is creating methods that mutate an object's state.
When working with JSON or CSV files, we'll often be working with objects that have a Python dict
object that defines the attributes. Using a dict
object to store an object's attributes has several consequences:
- We can trivially add and remove new attributes to the dictionary. We're not limited to simply setting and getting defined attributes. This makes it difficult for
mypy
to check our design carefully. TheTypedDict
type hint suggests a narrow domain of possible key values, but it doesn't prevent runtime use of unexpected keys.
...