delaying computation: lazy dictionaries in Python

posted on April 20, 2010 - tagged as: 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:

class LazyDictionary(object):

    def __init__(self, callback=dict): = None
        self.callback = callback

    def evaluate_callback(self): = self.callback()

    def __getitem__(self, name):
        if( is None):

    def __setitem__(self, name, value):
        if( is None):
        return, value)

    def __getattr__(self, name):
        if( is None):
        return getattr(, name)

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.

Comments !