Using properties for lazy attributes
In the Designing classes with lots of processing recipe we defined a class that eagerly computed a number of attributes of the data in a collection. The idea there was to compute the values as soon as possible, so that the attributes would have no further computational cost.
We described this as eager processing, since the work was done as soon as possible. The other approach is lazy processing, where the work is done as late as possible.
What if we have values that are used rarely, and are very expensive to compute? What can we do to minimize the up-front computation, and only compute values when they are truly needed?
Getting ready...
Let's say we've collected data using a Counter
object. For more information on the various collections, see Chapter 4, Built-in Data Structures – list, set, dict, specifically the Using set methods and operators and Avoiding mutable default values for function parameters recipes. In this case, the customers fall into eight...