self._check_size_limit()

def __setitem__(self, key, value):
    OrderedDict.__setitem__(self, key, value)
self._check_size_limit()

def _check_size_limit(self):
    if self.size_limit is not None:
        while len(self) > self.size_limit:
            self.popitem(last=False)
Limiting the size of a python dictionary