delaying computation: lazy dictionaries in Python
Yesterday I had a problem I ended up solving another way, but my first approach required a lazy dictionary. That is, a dictionary filled with the results of some other function, but also one that only evaluated this computation when the dictionary was first accessed. Here’s a silly example use case:
|
|
The real version I started to use (and then threw out because it was not needed) inherited from a dict instead of using delegation. At the time I thought this was necessary to get it to play nice with the Django templating system (I may have been wrong about this). Anyways, I like this version much better, and I view inheritance for the composition of behavior with growing skepticism each day.