There's more...

If we need to have stateful objects, we can add update methods that can change the Counter object. For example, we can introduce a method to add another value by delegating the work to the associated Counter. This switches the design pattern from a simple connection between computation and collection to a proper wrapper around the collection.

The method might look like this:

    def add(self, value): 
        self.raw_counter[value] += 1 
        self.mean = self.compute_mean() 
        self.stddev = self.compute_stddev() 

First, we've updated the state of the Counter. Then, we recomputed all of the derived values. This kind of processing might create tremendous computation overheads. There needs to be a compelling reason to recompute the mean and standard ...

Get Modern Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.