return super(cls, self).__init__(*args, **kwargs)
cls.__init__ = __init__

def remember(f):

    def _(self, key, *args, **kwargs):
        self.memory.add(key)
return f(self, key, *args, **kwargs)
return _
for method_name in ['__getitem__', '__contains__', 'get']:
    m = getattr(cls, method_name)
setattr(cls, method_name, remember(m))
return cls


class DictWithMemory(dict, metaclass=KeyRememberer):
    pass

def unused_keys(self):
    """
        Returns the list of unused keys.
        """
print('Used', self.memory)
return list(set(super(DictWithMemory, self).keys()).difference(self.memory))
Python dictionary with memory of keys that were accessed?