7
Complex Stateless Objects
Many of the examples we’ve looked at have either been functions using atomic (or scalar) objects, or relatively simple structures built from small tuples. We can often exploit Python’s immutable typing.NamedTuple
as a way to build complex data structures. The class-like syntax seems much easier to read than the older collections.namedtuple
syntax.
One of the beneficial features of object-oriented programming is the ability to create complex data structures incrementally. In some respects, an object can be viewed as a cache for results of functions; this will often fit well with functional design patterns. In other cases, the object paradigm provides for property methods that include sophisticated calculations to derive data from an object’s properties. Using properties of an otherwise immutable class is also a good fit for functional design ideas.
In this chapter, we’ll look at the following:
How we create and use
NamedTuple...